Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / nsgmls / Allocator.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: Allocator.C /main/1 1996/07/29 16:45:56 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #include "splib.h"
28 #include "Allocator.h"
29 #include "macros.h"
30
31 #ifdef SP_NAMESPACE
32 namespace SP_NAMESPACE {
33 #endif
34
35 Allocator::Allocator(size_t maxSize, unsigned blocksPerSegment)
36 : objectSize_(maxSize),
37   blocksPerSegment_(blocksPerSegment),
38   freeList_(0),
39   segments_(0)
40 {
41 }
42
43 Allocator::~Allocator()
44 {
45   SegmentHeader *p = segments_;
46   while (p) {
47     SegmentHeader *tem = p->next;
48     if (p->liveCount == 0)
49       ::operator delete(p);
50     else
51       p->freeList = 0;
52     p = tem;
53   }
54 }
55
56 void *Allocator::alloc(size_t sz)
57 {
58   if (sz > objectSize_)
59     tooBig(sz);
60   Block *tem = freeList_;
61   if (tem) {
62     tem->header.seg->liveCount += 1;
63     freeList_ = tem->next;
64     return &(tem->next);
65   }
66   else
67     return alloc1();
68 }
69
70 void *Allocator::allocSimple(size_t sz)
71 {
72   BlockHeader *p = (BlockHeader *)::operator new(sz + sizeof(BlockHeader));
73   p->seg = 0;
74   return p + 1;
75 }
76
77 void Allocator::free(void *p)
78 {
79   BlockHeader *b = ((BlockHeader *)p) - 1;
80   SegmentHeader *seg = b->seg;
81   if (seg == 0)
82     ::operator delete(b);
83   else {
84     Block **freeList = seg->freeList;
85     if (freeList == 0) {
86       seg->liveCount -= 1;
87       if (seg->liveCount == 0)
88         ::operator delete(seg);
89     }
90     else {
91       ((Block *)b)->next = *freeList;
92       *freeList = (Block *)b;
93       seg->liveCount -= 1;
94     }
95   }
96 }
97
98 void *Allocator::alloc1()
99 {
100   SegmentHeader *seg
101     = (SegmentHeader *)::operator new(sizeof(SegmentHeader)
102                                       + ((objectSize_ + sizeof(BlockHeader))
103                                          * blocksPerSegment_));
104   seg->next = segments_;
105   segments_ = seg;
106   seg->liveCount = 1;
107   seg->freeList = &freeList_;
108   char *p = (char *)(seg + 1);
109   Block *head = 0;
110   for (size_t n = blocksPerSegment_; n > 0; n--) {
111     ((Block *)p)->next = head;
112     ((Block *)p)->header.seg = seg;
113     head = (Block *)p;
114     p += sizeof(BlockHeader) + objectSize_;
115   }
116   freeList_ = head->next;
117   return &(head->next);
118 }
119
120 void Allocator::tooBig(size_t sz)
121 {
122   ASSERT(sz <= objectSize_);
123 }
124
125 #ifdef SP_NAMESPACE
126 }
127 #endif