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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/********************************************************************
* APRX -- 2nd generation APRS iGate and digi with *
* minimal requirement of esoteric facilities or *
* libraries of any kind beyond UNIX system libc. *
* *
* (c) Matti Aarnio - OH2MQK, 2007-2014 *
* *
********************************************************************/
#include "config.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
// #if defined(HAVE_PTHREAD_CREATE) && defined(ENABLE_PTHREAD)
// #include <pthread.h>
// #endif
#include "cellmalloc.h"
#define NO_MMAP_ON_CELLMALLOC
#define MEMDEBUG
/*
* cellmalloc() -- manages arrays of cells of data
*
*/
struct cellhead;
struct cellarena_t {
int cellsize;
int alignment;
int increment; /* alignment overhead applied.. */
int lifo_policy;
int minfree;
const char *arenaname;
// pthread_mutex_t mutex; // we have a mutex-less usage environment!
struct cellhead *free_head;
struct cellhead *free_tail;
int freecount;
int createsize;
#ifdef MEMDEBUG
int cellblocks_count;
#define CELLBLOCKS_MAX 40 /* track client cell allocator limit! */
char *cellblocks[CELLBLOCKS_MAX]; /* ref as 'char pointer' for pointer arithmetics... */
#endif
};
#define CELLHEAD_DEBUG 0
struct cellhead {
#if CELLHEAD_DEBUG == 1
struct cellarena_t *ca;
#endif
struct cellhead *next;
};
/*
* new_cellblock() -- must be called MUTEX PROTECTED
*
*/
int new_cellblock(cellarena_t *ca)
{
int i;
char *cb;
#ifdef MEMDEBUG /* External backing-store files, unique ones for each cellblock,
which at Linux names memory blocks in /proc/nnn/smaps "file"
with this filename.. */
int fd;
char name[2048];
sprintf(name, "/tmp/.-%d-%s-%d.mmap", getpid(), ca->arenaname, ca->cellblocks_count );
unlink(name);
fd = open(name, O_RDWR|O_CREAT, 644);
unlink(name);
if (fd >= 0) {
memset(name, 0, sizeof(name));
i = 0;
while (i < ca->createsize) {
int rc = write(fd, name, sizeof(name));
if (rc < 0) break;
i += rc;
}
}
cb = mmap( NULL, ca->createsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (fd >= 0)
close(fd);
#else
#ifndef MAP_ANON
# define MAP_ANON 0
#endif
#ifdef NO_MMAP_ON_CELLMALLOC
cb = malloc( ca->createsize );
#else
cb = mmap( NULL, ca->createsize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
#endif
#endif
if (cb == NULL || cb == (char*)-1)
return -1;
#ifdef MEMDEBUG
if (ca->cellblocks_count >= CELLBLOCKS_MAX) return -1;
ca->cellblocks[ca->cellblocks_count++] = cb;
#endif
for (i = 0; i <= ca->createsize-ca->increment; i += ca->increment) {
struct cellhead *ch = (struct cellhead *)(cb + i); /* pointer arithmentic! */
if (!ca->free_head) {
ca->free_head = ch;
} else {
ca->free_tail->next = ch;
}
ca->free_tail = ch;
ch->next = NULL;
#if CELLHEAD_DEBUG == 1
ch->ca = ca; // cellhead pointer space
#endif
ca->freecount += 1;
}
return 0;
}
/*
* cellinit() -- the main program calls this once for each used cell type/size
*
*/
cellarena_t *cellinit( const char *arenaname, const int cellsize, const int alignment, const int policy, const int createkb, const int minfree )
{
cellarena_t *ca = calloc(1, sizeof(*ca));
// int n;
ca->arenaname = arenaname;
#if CELLHEAD_DEBUG == 1
if (alignment < __alignof__(void*))
alignment = __alignof__(void*); // cellhead pointer space
#endif
ca->cellsize = cellsize;
ca->alignment = alignment;
ca->minfree = minfree;
#if CELLHEAD_DEBUG == 1
ca->increment = cellsize + sizeof(void*); // cellhead pointer space
#else
ca->increment = cellsize;
#endif
if ((cellsize % alignment) != 0) {
ca->increment += alignment - cellsize % alignment;
}
ca->lifo_policy = policy & CELLMALLOC_POLICY_LIFO;
ca->createsize = createkb * 1024;
#if !defined(MEMDEBUG) && defined(NO_MMAP_ON_CELLMALLOC)
ca->createsize -= 16;
#endif
// n = ca->createsize / ca->increment;
// hlog( LOG_DEBUG, "cellinit: %-12s block size %4d kB, cells/block: %d", arenaname, createkb, n );
// pthread_mutex_init(&ca->mutex, NULL);
new_cellblock(ca); /* First block of cells, not yet need to be mutex protected */
while (ca->freecount < ca->minfree)
new_cellblock(ca); /* more until minfree is full */
#if CELLHEAD_DEBUG == 1
// hlog(LOG_DEBUG, "cellinit() cellhead=%p", ca);
#endif
return ca;
}
inline void *cellhead_to_clientptr(struct cellhead *ch)
{
char *p = (char*)ch;
#if CELLHEAD_DEBUG == 1
p += sizeof(void*);
#endif
return p;
}
inline struct cellhead *clientptr_to_cellhead(void *v)
{
#if CELLHEAD_DEBUG == 1
struct cellhead *ch = (struct cellhead *)(((char*)v) - sizeof(void*));
#else
struct cellhead *ch = (struct cellhead*)v;
#endif
return ch;
}
void *cellmalloc(cellarena_t *ca)
{
void *cp;
struct cellhead *ch;
while (!ca->free_head || (ca->freecount < ca->minfree))
if (new_cellblock(ca)) {
// pthread_mutex_unlock(&ca->mutex);
return NULL;
}
/* Pick new one off the free-head ! */
ch = ca->free_head;
ca->free_head = ch->next;
ch->next = NULL;
cp = ch;
if (ca->free_head == NULL)
ca->free_tail = NULL;
ca->freecount -= 1;
// hlog(LOG_DEBUG, "cellmalloc(%p at %p) freecount %d", cellhead_to_clientptr(cp), ca, ca->freecount);
return cellhead_to_clientptr(cp);
}
/*
* cellmallocmany() -- give many cells in single lock region
*
*/
int cellmallocmany(cellarena_t *ca, void **array, int numcells)
{
int count;
struct cellhead *ch;
for (count = 0; count < numcells; ++count) {
while (!ca->free_head ||
ca->freecount < ca->minfree) {
/* Out of free cells ? alloc new set */
if (new_cellblock(ca)) {
/* Failed ! */
break;
}
}
/* Pick new one off the free-head ! */
ch = ca->free_head;
// hlog( LOG_DEBUG, "cellmallocmany(%d of %d); freecount %d; %p at %p",
// count, numcells, ca->freecount, cellhead_to_clientptr(ch), ca );
if (ch != NULL) { // should always be...
ca->free_head = ch->next;
ch->next = NULL;
}
if (ca->free_head == NULL)
ca->free_tail = NULL;
array[count] = cellhead_to_clientptr(ch);
ca->freecount -= 1;
}
return count;
}
void cellfree(cellarena_t *ca, void *p)
{
struct cellhead *ch = clientptr_to_cellhead(p);
ch->next = NULL;
#if CELLHEAD_DEBUG == 1
if (ch->ca != ca) {
// hlog(LOG_ERR, "cellfree(%p to %p) wrong cellhead->ca pointer %p", p, ca, ch->ca);
}
#endif
// hlog(LOG_DEBUG, "cellfree() %p to %p", p, ca);
if (ca->lifo_policy) {
/* Put the cell on free-head */
ch->next = ca->free_head;
ca->free_head = ch;
} else {
/* Put the cell on free-tail */
if (ca->free_tail)
ca->free_tail->next = ch;
ca->free_tail = ch;
if (!ca->free_head)
ca->free_head = ch;
ch->next = NULL;
}
ca->freecount += 1;
}
/*
* cellfreemany() -- release many cells in single lock region
*
*/
void cellfreemany(cellarena_t *ca, void **array, int numcells)
{
int count;
for (count = 0; count < numcells; ++count) {
struct cellhead *ch = clientptr_to_cellhead(array[count]);
#if CELLHEAD_DEBUG == 1
if (ch->ca != ca) {
// hlog(LOG_ERR, "cellfreemany(%p to %p) wrong cellhead->ca pointer %p", array[count], ca, ch->ca);
}
#endif
// hlog(LOG_DEBUG, "cellfreemany() %p to %p", ch, ca);
if (ca->lifo_policy) {
/* Put the cell on free-head */
ch->next = ca->free_head;
ca->free_head = ch;
} else {
/* Put the cell on free-tail */
if (ca->free_tail)
ca->free_tail->next = ch;
ca->free_tail = ch;
if (!ca->free_head)
ca->free_head = ch;
ch->next = NULL;
}
ca->freecount += 1;
}
}
|