1 // Copyright (c) 1994 James Clark 2 // See the file COPYING for copying permission. 3 #pragma ident "%Z%%M% %I% %E% SMI" 4 5 #ifndef Allocator_INCLUDED 6 #define Allocator_INCLUDED 1 7 8 #include <stddef.h> 9 10 #ifdef SP_NAMESPACE 11 namespace SP_NAMESPACE { 12 #endif 13 14 class SP_API Allocator { 15 public: 16 Allocator(size_t maxSize, unsigned blocksPerSegment); 17 ~Allocator(); 18 void *alloc(size_t); 19 static void *allocSimple(size_t); 20 static void free(void *); 21 22 // It would be nice to make these private, but some compilers have problems. 23 union ForceAlign { 24 unsigned long n; 25 struct SP_API { 26 char c; 27 } s; 28 char *cp; 29 long *lp; 30 }; 31 struct SegmentHeader; 32 union BlockHeader; 33 friend union BlockHeader; 34 union BlockHeader { 35 SegmentHeader *seg; 36 ForceAlign align; 37 }; 38 struct Block; 39 friend struct Block; 40 struct SP_API Block { 41 BlockHeader header; 42 Block *next; 43 }; 44 friend struct SegmentHeader; 45 struct SP_API SegmentHeader { 46 union { 47 Block **freeList; 48 ForceAlign align; 49 }; 50 unsigned liveCount; 51 SegmentHeader *next; 52 }; 53 private: 54 Allocator(const Allocator &); // undefined 55 Allocator &operator=(const Allocator &); // undefined 56 Block *freeList_; 57 size_t objectSize_; 58 unsigned blocksPerSegment_; 59 SegmentHeader *segments_; 60 void *alloc1(); 61 void tooBig(size_t); 62 }; 63 64 #ifdef SP_NAMESPACE 65 } 66 #endif 67 68 #endif /* not Allocator_INCLUDED */ 69