|
|
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.5 ! parser 8: $Id: pool_storage.h,v 1.4 2001/04/03 07:32:49 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:
1.4 paf 19: @todo implement at least simple suballocations
1.1 paf 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: }
1.5 ! parser 57:
! 58: /// @test implement
! 59: void register_cleanup(void (*cleanup) (void *), void *data) {
! 60:
! 61: }
! 62:
1.1 paf 63:
64: private:
65:
66: bool full() { return used==size; }
67: bool expand() {
1.2 paf 68: size_t new_size=size*3/2;
1.1 paf 69: void **new_ptrs=(void **)::realloc(ptrs, new_size*sizeof(void *));
70: if(new_ptrs) {
71: ptrs=new_ptrs;
72: size=new_size;
73: return true;
74: } else
75: return false;
76: }
77: };
78:
79:
80: #endif