|
|
1.1 paf 1: /** @file
2: Parser: ISAPI: pool storage class decl.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.2 ! paf 8: $Id: pool_storage.h,v 1.1 2001/03/23 19:25:33 paf Exp $
1.1 paf 9: */
10:
11: #ifndef PA_POOL_STORAGE_H
12: #define PA_POOL_STORAGE_H
13:
14: #include "pa_config_includes.h"
15:
16: /**
17: Dumb pool allocations accounter
18:
19: @todo implement at least simple suballocations
20: */
21: class Pool_storage {
22: public:
23:
24: void **ptrs;
25: size_t size;
26: size_t used;
27:
28: Pool_storage(size_t preallocate=10*0x400) :
29: ptrs((void **)::malloc(preallocate*sizeof(void *))),
30: size(0),
31: used(0) {
32: if(ptrs) // successfully preallocated?
33: size=preallocate;
34: }
35:
36: ~Pool_storage() {
37: return;
38: for(size_t i=0; i<used; i++)
39: free(ptrs[i]);
40: free(ptrs);
41: }
42:
43: void *malloc(size_t size) {
44: if(full())
45: if(!expand())
46: return 0;
47:
48: return ptrs[used++]=::malloc(size);
49: }
50: void *calloc(size_t size) {
51: if(full())
52: if(!expand())
53: return 0;
54:
55: return ptrs[used++]=::calloc(size, 1);
56: }
57:
58: private:
59:
60: bool full() { return used==size; }
61: bool expand() {
1.2 ! paf 62: size_t new_size=size*3/2;
1.1 paf 63: void **new_ptrs=(void **)::realloc(ptrs, new_size*sizeof(void *));
64: if(new_ptrs) {
65: ptrs=new_ptrs;
66: size=new_size;
67: return true;
68: } else
69: return false;
70: }
71: };
72:
73:
74: #endif