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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
|
/*
* $Id: $
* $Version: $
*
* Copyright (c) Tanel Tammet 2004,2005,2006,2007,2008,2009
*
* Contact: tanel.tammet@gmail.com
*
* This file is part of WhiteDB
*
* WhiteDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WhiteDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WhiteDB. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** @file dbmpool.c
* Allocating data using a temporary memory pool.
*
*/
/* ====== Includes =============== */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/shm.h>
#include <sys/errno.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#include "../config-w32.h"
#else
#include "../config.h"
#endif
#include "dballoc.h"
#include "dbmem.h"
#include "dbapi.h"
/* ====== Private headers and defs ======== */
#define NROF_SUBAREAS 100 // size of subarea array
#define MIN_FIRST_SUBAREA_SIZE 1024 // first free area minimum: if less asked, this given
#define ALIGNMENT_BYTES 4 // every val returned by wg_alloc_mpool is aligned to this
#define TYPEMASK 1 // memory pool convenience objects type mask for address
#define PAIRBITS 0 // memory pool convenience objects type bit for pairs (lists)
#define ATOMBITS 1 // memory pool convenience objects type bit for atoms (strings etc)
/** located inside mpool_header: one single memory subarea header
*
*
*/
typedef struct _wg_mpoolsubarea_header {
int size; /** size of subarea in bytes */
void* area_start; /** pointer to the first byte of the subarea */
void* area_end; /** pointer to the first byte after the subarea */
} mpool_subarea_header;
/** memory pool management data
* stored in the beginning of the first segment of mempool
*
*/
typedef struct {
void* freeptr; /** pointer to the next free location in the pool */
int cur_subarea; /** index of the currently used subarea in the subarea_table (starts with 0) */
int nrof_subareas; /** full nr of rows in the subarea table */
mpool_subarea_header subarea_table[NROF_SUBAREAS]; /** subarea information (mpool_subarea_header) table */
} mpool_header;
/* ======= Private protos ================ */
static int extend_mpool(void* db, void* mpool, int minbytes);
static int show_mpool_error(void* db, char* errmsg);
static int show_mpool_error_nr(void* db, char* errmsg, int nr);
static void wg_mpool_print_aux(void* db, void* ptr, int depth, int pflag);
/* ====== Functions for mpool creating/extending/allocating/destroying ============== */
/** create and initialise a new memory pool
*
* initial pool has at least origbytes of free space
* mpool is extended automatically later when space used up
* returns void* pointer to mpool if OK, NULL if failure
*
* does a single malloc (latex extensions do further mallocs)
*/
void* wg_create_mpool(void* db, int origbytes) {
int bytes;
void* mpool;
mpool_header* mpoolh;
int puresize;
void* nextptr;
int i;
if (origbytes<MIN_FIRST_SUBAREA_SIZE+ALIGNMENT_BYTES)
bytes=sizeof(mpool_header)+MIN_FIRST_SUBAREA_SIZE+ALIGNMENT_BYTES;
else
bytes=sizeof(mpool_header)+origbytes+ALIGNMENT_BYTES;
puresize=bytes-sizeof(mpool_header);
mpool=malloc(bytes);
if (mpool==NULL) {
show_mpool_error_nr(db,
" cannot create an mpool with size: ",origbytes);
return NULL;
}
mpoolh=(mpool_header*)mpool;
nextptr=(void*)(((char*)mpool)+sizeof(mpool_header));
// set correct alignment for nextptr
i=((size_t)nextptr)%ALIGNMENT_BYTES;
if (i!=0) nextptr=((char*)nextptr)+(ALIGNMENT_BYTES-i);
// aligment now ok
(mpoolh->freeptr)=nextptr;
(mpoolh->cur_subarea)=0;
((mpoolh->subarea_table)[0]).size=puresize;
((mpoolh->subarea_table)[0]).area_start=mpool;
((mpoolh->subarea_table)[0]).area_end=(void*)(((char*)mpool)+bytes);
return mpool;
}
/** extend an existing memory pool
*
* called automatically when mpool space used up
* does one malloc for a new subarea
*
*/
static int extend_mpool(void* db, void* mpool, int minbytes) {
int cursize;
int bytes;
void* subarea;
mpool_header* mpoolh;
int i;
void* nextptr;
mpoolh=(mpool_header*)mpool;
cursize=((mpoolh->subarea_table)[(mpoolh->cur_subarea)]).size;
bytes=cursize;
for(i=0;i<100;i++) {
bytes=bytes*2;
if (bytes>=(minbytes+ALIGNMENT_BYTES)) break;
}
subarea=malloc(bytes);
if (subarea==NULL) {
show_mpool_error_nr(db,
" cannot extend mpool to size: ",minbytes);
return -1;
}
(mpoolh->freeptr)=subarea;
(mpoolh->cur_subarea)++;
((mpoolh->subarea_table)[mpoolh->cur_subarea]).size=bytes;
nextptr=subarea;
// set correct alignment for nextptr
i=((size_t)nextptr)%ALIGNMENT_BYTES;
if (i!=0) nextptr=((char*)nextptr)+(ALIGNMENT_BYTES-i);
// aligment now ok
(mpoolh->freeptr)=nextptr;
((mpoolh->subarea_table)[mpoolh->cur_subarea]).area_start=subarea;
((mpoolh->subarea_table)[mpoolh->cur_subarea]).area_end=(void*)(((char*)subarea)+bytes);
return 0;
}
/** free the whole memory pool
*
* frees all the malloced subareas and initial mpool
*
*/
void wg_free_mpool(void* db, void* mpool) {
int i;
mpool_header* mpoolh;
mpoolh=(mpool_header*)mpool;
i=mpoolh->cur_subarea;
for(;i>0;i--) {
free(((mpoolh->subarea_table)[i]).area_start);
}
free(mpool);
}
/** allocate bytes from a memory pool: analogous to malloc
*
* mpool is extended automatically if not enough free space present
* returns void* pointer to a memory block if OK, NULL if failure
*
*/
void* wg_alloc_mpool(void* db, void* mpool, int bytes) {
void* curptr;
void* nextptr;
mpool_header* mpoolh;
void* curend;
int tmp;
int i;
if (bytes<=0) {
show_mpool_error_nr(db,
" trying to allocate too little from mpool: ",bytes);
return NULL;
}
if (mpool==NULL) {
show_mpool_error(db," mpool passed to wg_alloc_mpool is NULL ");
return NULL;
}
mpoolh=(mpool_header*)mpool;
nextptr=(void*)(((char*)(mpoolh->freeptr))+bytes);
curend=((mpoolh->subarea_table)[(mpoolh->cur_subarea)]).area_end;
if (nextptr>curend) {
tmp=extend_mpool(db,mpool,bytes);
if (tmp!=0) {
show_mpool_error_nr(db," cannot extend mpool size by: ",bytes);
return NULL;
}
nextptr=((char*)(mpoolh->freeptr))+bytes;
}
curptr=mpoolh->freeptr;
// set correct alignment for nextptr
i=((size_t)nextptr)%ALIGNMENT_BYTES;
if (i!=0) nextptr=((char*)nextptr)+(ALIGNMENT_BYTES-i);
// alignment now ok
mpoolh->freeptr=nextptr;
return curptr;
}
/* ====== Convenience functions for using data allocated from mpool ================= */
/*
Core object types are pairs and atoms plus 0 (NULL).
Lists are formed by pairs of gints. Each pair starts at address with two last bits 0.
The first element of the pair points to the contents of the cell, the second to rest.
Atoms may contain strings, ints etc etc. Each atom starts at address with last bit 1.
The first byte of the atom indicates its type. The following bytes are content, always
encoded as a 0-terminated string or TWO consequent 0-terminated strings.
The atom type byte contains dbapi.h values:
STRING, CONVERSION TO BE DETERMINED LATER: 0
#define WG_NULLTYPE 1
#define WG_RECORDTYPE 2
#define WG_INTTYPE 3
#define WG_DOUBLETYPE 4
#define WG_STRTYPE 5
#define WG_XMLLITERALTYPE 6
#define WG_URITYPE 7
#define WG_BLOBTYPE 8
#define WG_CHARTYPE 9
#define WG_FIXPOINTTYPE 10
#define WG_DATETYPE 11
#define WG_TIMETYPE 12
#define WG_ANONCONSTTYPE 13
#define WG_VARTYPE 14
#define WG_ILLEGAL 0xff
Atom types 5-8 (strings, xmlliterals, uris, blobs) contain TWO
consequent strings, first the main, terminating 0, then the
second (lang, namespace etc) and the terminating 0. Two terminating
0-s after the first indicates the missing second string (NULL).
Other types are simply terminated by two 0-s.
*/
// ------------- pairs ----------------
int wg_ispair(void* db, void* ptr) {
return (ptr!=NULL && ((((gint)ptr)&TYPEMASK)==PAIRBITS));
}
void* wg_mkpair(void* db, void* mpool, void* x, void* y) {
void* ptr;
ptr=wg_alloc_mpool(db,mpool,sizeof(gint)*2);
if (ptr==NULL) {
show_mpool_error(db,"cannot create a pair in mpool");
return NULL;
}
*((gint*)ptr)=(gint)x;
*((gint*)ptr+1)=(gint)y;
return ptr;
}
void* wg_first(void* db, void* ptr) {
return (void*)(*((gint*)ptr));
}
void* wg_rest(void* db, void *ptr) {
return (void*)(*((gint*)ptr+1));
}
int wg_listtreecount(void* db, void *ptr) {
if (wg_ispair(db,ptr))
return wg_listtreecount(db,wg_first(db,ptr)) + wg_listtreecount(db,wg_rest(db,ptr));
else
return 1;
}
// ------------ atoms ------------------
int wg_isatom(void* db, void* ptr) {
return (ptr!=NULL && ((((gint)ptr)&TYPEMASK)==ATOMBITS));
}
void* wg_mkatom(void* db, void* mpool, int type, char* str1, char* str2) {
char* ptr;
char* curptr;
int size=2;
if (str1!=NULL) size=size+strlen(str1);
size++;
if (str2!=NULL) size=size+strlen(str2);
size++;
ptr=(char*)(wg_alloc_mpool(db,mpool,size));
if (ptr==NULL) {
show_mpool_error(db,"cannot create an atom in mpool");
return NULL;
}
ptr++; // shift one pos right to set address last byte 1
curptr=ptr;
*curptr=(char)type;
curptr++;
if (str1!=NULL) {
while((*curptr++ = *str1++));
} else {
*curptr=(char)0;
curptr++;
}
if (str2!=NULL) {
while((*curptr++ = *str2++));
} else {
*curptr=(char)0;
curptr++;
}
return ptr;
}
int wg_atomtype(void* db, void* ptr) {
if (ptr==NULL) return 0;
else return (gint)*((char*)ptr);
}
char* wg_atomstr1(void* db, void* ptr) {
if (ptr==NULL) return NULL;
if (*(((char*)ptr)+1)==(char)0) return NULL;
else return ((char*)ptr)+1;
}
char* wg_atomstr2(void* db, void* ptr) {
if (ptr==NULL) return NULL;
ptr=(char*)ptr+strlen((char*)ptr)+1;
if (*(((char*)ptr)+1)==(char)0) return NULL;
else return ((char*)ptr);
}
// ------------ printing ------------------
void wg_mpool_print(void* db, void* ptr) {
wg_mpool_print_aux(db,ptr,0,1);
}
static void wg_mpool_print_aux(void* db, void* ptr, int depth, int pflag) {
int type;
char* p;
int count;
int ppflag=0;
int i;
void *curptr;
if (ptr==NULL) {
printf("()");
} else if (wg_isatom(db,ptr)) {
type=wg_atomtype(db,ptr);
switch (type) {
case 0: printf("_:"); break;
case WG_NULLTYPE: printf("n:"); break;
case WG_RECORDTYPE: printf("r:"); break;
case WG_INTTYPE: printf("i:"); break;
case WG_DOUBLETYPE: printf("d:"); break;
case WG_STRTYPE: printf("s:"); break;
case WG_XMLLITERALTYPE: printf("x:"); break;
case WG_URITYPE: printf("u:"); break;
case WG_BLOBTYPE: printf("b:"); break;
case WG_CHARTYPE: printf("c:"); break;
case WG_FIXPOINTTYPE: printf("f:"); break;
case WG_DATETYPE: printf("date:"); break;
case WG_TIMETYPE: printf("time:"); break;
case WG_ANONCONSTTYPE: printf("a:"); break;
case WG_VARTYPE: printf("?:"); break;
default: printf("!:");
}
p=wg_atomstr1(db,ptr);
if (p!=NULL) {
if (strchr(p,' ')!=NULL || strchr(p,'\n')!=NULL || strchr(p,'\t')!=NULL) {
printf("\"%s\"",p);
} else {
printf("%s",p);
}
} else {
printf("\"\"");
}
p=wg_atomstr2(db,ptr);
if (p!=NULL) {
if (strchr(p,' ')!=NULL || strchr(p,'\n')!=NULL || strchr(p,'\t')!=NULL) {
printf("^^\"%s\"",p);
} else {
printf("^^%s",p);
}
}
} else {
if (pflag && wg_listtreecount(db,ptr)>10) ppflag=1;
printf ("(");
for(curptr=ptr, count=0;curptr!=NULL && !wg_isatom(db,curptr);curptr=wg_rest(db,curptr), count++) {
if (count>0) {
if (ppflag) {
printf("\n");
for(i=0;i<depth;i++) printf(" ");
}
printf(" ");
}
wg_mpool_print_aux(db,wg_first(db,curptr),depth+1,0);
}
if (wg_isatom(db,curptr)) {
printf(" . ");
wg_mpool_print_aux(db,curptr,depth+1,ppflag);
}
printf (")");
if (ppflag) printf("\n");
}
}
// ------------- ints ---------------------
// ------------- floats --------------------
/* ============== error handling ==================== */
/** called with err msg when an mpool allocation error occurs
*
* may print or log an error
* does not do any jumps etc
*/
static int show_mpool_error(void* db, char* errmsg) {
#ifdef WG_NO_ERRPRINT
#else
fprintf(stderr,"db memory pool allocation error: %s\n",errmsg);
#endif
return -1;
}
/** called with err msg and err nr when an mpool allocation error occurs
*
* may print or log an error
* does not do any jumps etc
*/
static int show_mpool_error_nr(void* db, char* errmsg, int nr) {
#ifdef WG_NO_ERRPRINT
#else
fprintf(stderr,"db memory pool allocation error: %s %d\n",errmsg,nr);
#endif
return -1;
}
#ifdef __cplusplus
}
#endif
|