1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/* chunk.c 0.1 (c) 1999/1/30 Tom Wheeley <tomw@tsys.demon.co.uk> */
/* This code is placed under the GNU Public Licence */
/*
* Chunks are designed to store large numbers of small static parts of
* memory, for example hash table keys, symbol names in a symbol table,
* linked list / tree nodes etc.
*/
/*
* a chunk is stored as a series of blocks, each of which is packed with
* several objects allocated using ch_malloc(). Maintenance of these blocks
* is performed transparently, the user only calling ch_new() to start off
* each chunk, ch_malloc() to allocate an object in that chunk, and finally
* ch_free() to delete that chunk, and all the objects contained within it.
*/
/* do not use realloc() / free() on the pointers returned by ch_malloc()
* your program will surely suffer a segmentation fault!
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include "chunk.h"
/*
* ch_new
*
* creates a new chunk block. Must be called first to initialise a block
* for subsequent calls to ch_malloc()
*
* size is the size of the large blocks of memory the word's are stored
* in. No word can be larger than `size', although you shouldn't even
* consider a word length approaching even a fraction of `size'.
*/
chunk_t *ch_new(size_t size)
{
chunk_t *ch;
ch = malloc(sizeof (chunk_t));
if (!ch)
return NULL;
ch->size = CH_ALIGN(size);
ch->free = ch->size;
ch->bottom = malloc(ch->size);
ch->top = ch->bottom;
ch->next = NULL;
if ( ! ch->bottom) {
free(ch);
return NULL;
}
return ch;
}
/* version of ch_new() which will crash and burn if malloc() fails */
chunk_t *xch_new(size_t size)
{
chunk_t *ptr;
ptr = ch_new(size);
if (!ptr) {
fprintf(stderr, "fatal error: unable to allocate memory\n");
exit(EXIT_FAILURE);
}
return ptr;
}
/*
* ch_malloc
*
* use in place of malloc when allocating a small piece of memory.
*
*/
void *ch_malloc(size_t numbytes, chunk_t *chunk)
{
chunk_t *ch;
void *ptr;
if (!chunk) {
errno = ENOMEM;
return NULL;
}
numbytes = CH_ALIGN(numbytes);
/*
* we could just add a new chunk to the linked list that _is_ large
* enough, but you shouldn't be putting objects of this size in chunks
* anyway! These things are designed for hundreds of objects
*/
if (numbytes > chunk->size) {
errno = E2BIG;
return NULL;
}
/* iterate through the list of blocks looking for one with enough space
* left. If none, then create a new one at the end
*/
for(ch = chunk; ch->free < numbytes; ch=ch->next) {
if (!ch->next) {
ch->next = ch_new(ch->size);
}
if (!ch->next) {
return NULL;
}
}
/* sanity check */
assert(ch && numbytes <= ch->free);
ptr = ch->top;
ch->free -= numbytes;
(char *)ch->top += numbytes;
return ptr;
}
/* version of ch_malloc() which will crash and burn if malloc() fails */
void *xch_malloc(size_t numbytes, chunk_t *chunk)
{
void *ptr;
ptr = ch_malloc(numbytes, chunk);
if (!ptr) {
fprintf(stderr, "fatal error: unable to allocate memory\n");
exit(EXIT_FAILURE);
}
return ptr;
}
/*
* ch_free
*
* frees an entire chunk in one go.
*
*/
void ch_free(chunk_t *chunk)
{
chunk_t *dead;
/* loop through, removing the data and the linked list blocks */
for (;chunk;) {
dead = chunk;
chunk = dead->next;
if (dead->bottom)
free(dead->bottom);
if (dead)
free(dead);
}
}
/* simply calls ch_free. here to complete the namespace */
void xch_free(chunk_t *chunk)
{
ch_free(chunk);
}
int ch_stat(chunk_t *chunk, int *num_blocks_r, size_t *block_size_r,
size_t *mem_used_r, size_t *mem_wasted_r)
{
chunk_t *ch;
int num = 0;
size_t used = 0;
size_t wasted = 0;
if (!chunk)
return -1;
/* only work these things out if people want to know */
if (num_blocks_r || mem_used_r || mem_wasted_r) {
for (ch=chunk; ch; ch=ch->next) {
num++;
used += ch->size - ch->free;
if (ch->next) {
wasted += ch->free;
}
}
}
/* this way, if people don't care about a particular statistic they can
* just pass NULL. Also helps prevent segfaults
*/
if (num_blocks_r)
*num_blocks_r = num;
if (block_size_r)
*block_size_r = chunk->size; /* only safe way to get this */
if (mem_used_r)
*mem_used_r = used;
if (mem_wasted_r)
*mem_wasted_r = wasted;
return 0;
}
/* end of chunk.c */
|