Annotation of parser3/src/targets/cgi/pool_storage.h, revision 1.1
1.1 ! parser 1: /** @file
! 2: Parser: CGI: pool storage class decl.
! 3:
! 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
! 5: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
! 6:
! 7: $Id: pool_storage.h,v 1.10 2001/10/13 17:17:37 parser Exp $
! 8: */
! 9:
! 10: #ifndef PA_POOL_STORAGE_H
! 11: #define PA_POOL_STORAGE_H
! 12:
! 13: #include "pa_config_includes.h"
! 14:
! 15: /**
! 16: Dumb pool allocations accounter
! 17:
! 18: @todo implement at least simple suballocations
! 19: */
! 20: class Pool_storage {
! 21:
! 22: template<typename T> class List {
! 23: friend Pool_storage;
! 24: public:
! 25: List(int preallocate) :
! 26: items((T *)::malloc(preallocate*sizeof(T))),
! 27: allocated(0),
! 28: used(0) {
! 29: if(items) // successfully preallocated?
! 30: allocated=preallocate;
! 31: }
! 32:
! 33: ~List() {
! 34: ::free(items);
! 35: }
! 36:
! 37: bool add(T item) {
! 38: if(full())
! 39: if(!expand())
! 40: return false;
! 41:
! 42: items[used++]=item;
! 43: return true;
! 44: }
! 45:
! 46: private:
! 47:
! 48: bool full() { return used==allocated; }
! 49: bool expand() {
! 50: size_t new_allocated=allocated*3/2;
! 51: T *new_items=(T *)::realloc(items, new_allocated*sizeof(T));
! 52: if(new_items) {
! 53: items=new_items;
! 54: allocated=new_allocated;
! 55: return true;
! 56: } else
! 57: return false;
! 58: }
! 59:
! 60: private:
! 61:
! 62: T *items;
! 63: size_t allocated;
! 64: size_t used;
! 65:
! 66: };
! 67:
! 68: struct Cleanup_struct {
! 69: void (*cleanup) (void *);
! 70: void *data;
! 71: };
! 72:
! 73:
! 74: public:
! 75:
! 76: Pool_storage() :
! 77: cleanups(100) {
! 78: }
! 79:
! 80: bool register_cleanup(void (*cleanup) (void *), void *data) {
! 81: Cleanup_struct item={cleanup, data};
! 82: return cleanups.add(item)!=0;
! 83: }
! 84:
! 85: ~Pool_storage() {
! 86: size_t top;
! 87:
! 88: // cleanups first, because they use some object's memory pointers
! 89: // Cleanup_structs
! 90: for(top=cleanups.used; top; ) {
! 91: Cleanup_struct& item=cleanups.items[--top];
! 92: item.cleanup(item.data);
! 93: }
! 94: }
! 95:
! 96: private:
! 97:
! 98: List<Cleanup_struct> cleanups;
! 99:
! 100: };
! 101:
! 102:
! 103: #endif
E-mail: