home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / include / ds.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.2 KB  |  268 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef __DS_h_
  20. #define __DS_h_
  21. #ifdef XP_WIN32
  22. #include <windows.h>
  23. #endif /* XP_WIN32 */
  24.  
  25. #ifdef XP_OS2
  26. #define INCL_WIN
  27. #define INCL_GPI
  28. #define TID OS2TID   /* global rename in OS2 H's!               */
  29. #include <os2.h>
  30. #undef TID           /* and restore                             */
  31. #endif
  32.  
  33. #include "xp_mcom.h"
  34.  
  35. XP_BEGIN_PROTOS
  36.  
  37. /* Typedefs */
  38. typedef struct DSArrayStr DSArray;
  39. typedef struct DSLinkStr DSLink;
  40. typedef struct DSListStr DSList;
  41. typedef struct DSArenaStr DSArena;
  42.  
  43. #define DS_MIN(a,b) ((a)<(b)?(a):(b))
  44. #define DS_MAX(a,b) ((a)>(b)?(a):(b))
  45.  
  46. /*
  47. ** Your basic boolean. Done as an enum to cause compiler warnings when a
  48. ** boolean procedure doesn't return the right value.
  49. ** LISA SEZ: Please do not use this; use PRBool instead.  Eventually
  50. ** (as soon as I can "make it so") DSBool is going away in favor of PRBool.
  51. */
  52. typedef enum DSBoolEnum {
  53.     DSTrue = 1,
  54.     DSFalse = 0
  55. } DSBool;
  56.  
  57. /*
  58. ** A status code. Status's are used by procedures that return status
  59. ** values. Again the motivation is so that a compiler can generate
  60. ** warnings when return values are wrong. Correct testing of status codes:
  61. **
  62. **    DSStatus rv;
  63. **    rv = some_function (some_argument);
  64. **    if (rv != DSSuccess)
  65. **        do_an_error_thing();
  66. **
  67. */
  68. typedef enum DSStatusEnum {
  69.     DSWouldBlock = -2,
  70.     DSFailure = -1,
  71.     DSSuccess = 0
  72. } DSStatus;
  73.  
  74. /*
  75. ** A comparison code. Used for procedures that return comparision
  76. ** values. Again the motivation is so that a compiler can generate
  77. ** warnings when return values are wrong.
  78. */
  79. typedef enum DSComparisonEnum {
  80.     DSLessThan = -1,
  81.     DSEqual = 0,
  82.     DSGreaterThan = 1
  83. } DSComparison;
  84.  
  85. typedef void (*DSElementFreeFunc)(void *e1, DSBool freeit);
  86. typedef int (*DSElementCompareFunc)(void *e1, void *e2);
  87.  
  88. /************************************************************************/
  89.  
  90. /*
  91. ** Simple variable length array of pointers. The array keeps a NULL
  92. ** pointer at the end of the array.
  93. */
  94. struct DSArrayStr {
  95.     void **things;
  96.     DSElementFreeFunc freeElement;
  97. };
  98.  
  99. extern DSArray *DS_CreateArray(int slots);
  100. extern DSStatus DS_GrowArray(DSArray *da, int slots);
  101. extern void DS_SetArrayMethods(DSArray *da, DSElementFreeFunc free);
  102. extern void DS_DestroyArray(DSArray *da, DSBool freeit);
  103. extern void DS_Sort(DSArray *da, DSElementCompareFunc compare);
  104. extern int DS_Elements(DSArray *da);
  105. extern DSStatus DS_AddElement(DSArray *da, void *element);
  106. extern void DS_RemoveElement(DSArray *da, void *element);
  107.  
  108. /************************************************************************/
  109.  
  110. /*
  111. ** Circular linked list. Each link contains a pointer to the object that
  112. ** is actually in the list.
  113. */
  114. struct DSLinkStr {
  115.     DSLink *next;
  116.     DSLink *prev;
  117.     void *thing;
  118. };
  119.  
  120. struct DSListStr {
  121.     DSLink link;
  122. };
  123.  
  124. #define DS_InitList(lst)         \
  125. {                     \
  126.     (lst)->link.next = &(lst)->link; \
  127.     (lst)->link.prev = &(lst)->link; \
  128.     (lst)->link.thing = 0;         \
  129. }
  130.  
  131. #define DS_ListEmpty(lst) \
  132.     ((lst)->link.next == &(lst)->link)
  133.  
  134. #define DS_ListHead(lst) \
  135.     ((lst)->link.next)
  136.  
  137. #define DS_ListTail(lst) \
  138.     ((lst)->link.prev)
  139.  
  140. #define DS_ListIterDone(lst,lnk) \
  141.     ((lnk) == &(lst)->link)
  142.  
  143. #define DS_AppendLink(lst,lnk)        \
  144. {                    \
  145.     (lnk)->next = &(lst)->link;        \
  146.     (lnk)->prev = (lst)->link.prev; \
  147.     (lst)->link.prev->next = (lnk); \
  148.     (lst)->link.prev = (lnk);        \
  149. }
  150.  
  151. #define DS_InsertLink(lst,lnk)        \
  152. {                    \
  153.     (lnk)->next = (lst)->link.next; \
  154.     (lnk)->prev = &(lst)->link;        \
  155.     (lst)->link.next->prev = (lnk); \
  156.     (lst)->link.next = (lnk);        \
  157. }
  158.  
  159. #define DS_RemoveLink(lnk)         \
  160. {                     \
  161.     (lnk)->next->prev = (lnk)->prev; \
  162.     (lnk)->prev->next = (lnk)->next; \
  163.     (lnk)->next = 0;             \
  164.     (lnk)->prev = 0;             \
  165. }
  166.  
  167. extern DSLink *DS_NewLink(void *thing);
  168. extern DSLink *DS_FindLink(DSList *lst, void *thing);
  169. extern void DS_DestroyLink(DSLink *lnk, DSBool freeit);
  170.  
  171. /************************************************************************/
  172.  
  173. /*
  174. ** Memory manager
  175. */
  176.  
  177. /*
  178. ** at one time XP_Block was a float* to force clients to cast things
  179. ** before use. Now DSBlock is defined since that will be most convenient
  180. ** for almost all uses.
  181. */
  182.  
  183. typedef unsigned char *DSBlock;
  184. /*
  185. ** Allocate some memory. Always allocates at least one byte of memory.
  186. */
  187. extern void *DS_Alloc(size_t bytes);
  188.  
  189. /*
  190. ** Reallocate some memory, growing or shrinking the memory.
  191. */
  192. extern void *DS_Realloc(void *oldptr, size_t bytes);
  193.  
  194. /*
  195. ** Allocate and then zero some memory. Always allocates at least one byte
  196. ** of memory.
  197. */
  198. extern void *DS_Zalloc(size_t bytes);
  199.  
  200. /*
  201. ** Allocate a block of memory. Always allocates at least one byte of
  202. ** memory.
  203. */
  204. extern DSBlock DS_AllocBlock(size_t bytes);
  205.  
  206. /*
  207. ** Reallocate a block of memory, growing or shrinking the memory block.
  208. */
  209. extern DSBlock DS_ReallocBlock(DSBlock block, size_t newBytes);
  210.  
  211. /*
  212. ** Free a block of memory. Safe to use on null pointers.
  213. */
  214. extern void DS_FreeBlock(DSBlock block);
  215.  
  216. /*
  217. ** Free a chunk of memory. Safe to use on null pointers.
  218. */
  219. extern void DS_Free(void *ptr);
  220.  
  221. /*
  222. ** Zero and then free a chunk of memory. Safe to use on null pointers.
  223. */
  224. extern void DS_Zfree(void *ptr, size_t bytes);
  225.  
  226. /*
  227.  * Low cost Malloc Arenas.
  228.  *
  229.  * The chunks are a linked list.
  230.  * The beginning of each chunk is a pointer to the next chunk.
  231.  */
  232. struct DSArenaStr {
  233.     unsigned long chunkSize;    /* size of each chunk */
  234.     unsigned int refCount;    /* reference count */
  235.     void ** firstChunk;        /* pointer to first chunk */
  236.     void ** lastChunk;        /* pointer to last chunk */
  237.     void * pLast;        /* last item allocated */
  238.     void * pCur;        /* beginning of free area */
  239.     void * pCurEnd;        /* end of free area in current chunk */
  240. };
  241.  
  242. /* make a new arena */
  243. extern DSArena *
  244. DS_NewArena(unsigned long chunkSize);
  245.  
  246. /* destroy an arena, and free all memory associated with it */
  247. extern void
  248. DS_FreeArena(DSArena *arena, DSBool zero);
  249.  
  250. /* malloc a chunk of data from the arena */
  251. extern void *
  252. DS_ArenaAlloc(DSArena *arena, unsigned long size);
  253.  
  254. /* malloc a chunk of data from the arena, zero filling it */
  255. extern void *
  256. DS_ArenaZalloc(DSArena *arena, unsigned long size);
  257.  
  258. /* change the size of an object, works best if it was the last object
  259.  * allocated
  260.  */
  261. extern void *
  262. DS_ArenaGrow(DSArena *arena, void *pOld, unsigned long oldsize,
  263.          unsigned long newsize);
  264.  
  265. XP_END_PROTOS
  266.  
  267. #endif /* __DS_h_ */
  268.