1 // STRALL.C String allocator
2 // Rex E. Bradford (REX)
4 // This module implements a string storage manager, based on
5 // the homogenous heap module. An array of homogenous heaps
6 // for various string sizes is kept. If a string is larger
7 // than the item size of the largest string heap, Malloc() and
8 // Free() are used for it. Otherwise, the appropriate homogenous
9 // heap is used for the string's storage.
11 // Note that, on average, 1/4 of the space allocated for a string
12 // is excess space and is wasted. Hey, life's rough.
15 * $Header: r:/prj/lib/src/dstruct/rcs/strall.c 1.3 1993/04/30 11:04:51 rex Exp $
17 * Revision 1.3 1993/04/30 11:04:51 rex
18 * Made each of 5 string heaps use a different # of items per chunk, so that
19 * they are roughly balanced in total bytes per string chunk (.5K or 1K each)
21 * Revision 1.2 1993/04/27 09:53:02 rex
22 * Fixed bug, StringAlloc() didn't return value for large strings!
24 * Revision 1.1 1993/04/19 09:29:04 rex
35 // This module maintains 5 homogenous heaps, for string sizes
36 // including of '\0' of up to 8, 16, 32, 64, and 128 bytes. Larger
37 // strings are handled with Malloc() and Free().
39 #define NUM_STRING_HEAP_SIZES 5
40 short strallSizeStrings
[NUM_STRING_HEAP_SIZES
] = {8,16,32,64,128};
41 short strallNumItems
[NUM_STRING_HEAP_SIZES
] = {64,32,32,16,8};
42 HheapHead hheapStrings
[NUM_STRING_HEAP_SIZES
];
43 HheapHead
*pheapHeadStrings
[NUM_STRING_HEAP_SIZES
];
45 // ---------------------------------------------------------
46 // STRING STORAGE ALLOCATOR & DEALLOCATOR
47 // ---------------------------------------------------------
49 // StringAlloc() allocates a string according to its size.
51 // s = ptr to string to be copied into string storage.
53 // Returns: ptr to allocated string in string block (or Malloc()'ed)
54 // if larger than largest slot size.
56 char *StringAlloc(char *s
)
58 static bool heapsNotInitialized
= TRUE
;
64 // Make sure our heaps are initialized
66 if (heapsNotInitialized
)
68 phh
= &hheapStrings
[0];
69 for (itable
= 0; itable
< NUM_STRING_HEAP_SIZES
; itable
++, phh
++)
71 pheapHeadStrings
[itable
] = phh
;
72 HheapInit(phh
, strallSizeStrings
[itable
], strallNumItems
[itable
],
75 heapsNotInitialized
= FALSE
;
78 // Calculate length of string, find proper table, alloc string
81 for (itable
= 0; itable
< NUM_STRING_HEAP_SIZES
; itable
++)
83 if (len
<= strallSizeStrings
[itable
])
85 p
= HheapAlloc(pheapHeadStrings
[itable
]);
91 // If too big for biggest table, just Malloc() it & copy string in
98 // --------------------------------------------------------------
100 // StringFree() frees a string.
102 // s = ptr to string to be freed, gotten from StringAlloc()
104 void StringFree(char *s
)
109 // Calculate length of string, find proper heap table, free string
112 for (itable
= 0; itable
< NUM_STRING_HEAP_SIZES
; itable
++)
114 if (len
<= strallSizeStrings
[itable
])
116 HheapFree(pheapHeadStrings
[itable
], s
);
121 // If larger than largest table size, just use Free()