2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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.
28 #include "Allocator.h"
32 namespace SP_NAMESPACE {
35 Allocator::Allocator(size_t maxSize, unsigned blocksPerSegment)
36 : objectSize_(maxSize),
37 blocksPerSegment_(blocksPerSegment),
43 Allocator::~Allocator()
45 SegmentHeader *p = segments_;
47 SegmentHeader *tem = p->next;
48 if (p->liveCount == 0)
56 void *Allocator::alloc(size_t sz)
60 Block *tem = freeList_;
62 tem->header.seg->liveCount += 1;
63 freeList_ = tem->next;
70 void *Allocator::allocSimple(size_t sz)
72 BlockHeader *p = (BlockHeader *)::operator new(sz + sizeof(BlockHeader));
77 void Allocator::free(void *p)
79 BlockHeader *b = ((BlockHeader *)p) - 1;
80 SegmentHeader *seg = b->seg;
84 Block **freeList = seg->freeList;
87 if (seg->liveCount == 0)
88 ::operator delete(seg);
91 ((Block *)b)->next = *freeList;
92 *freeList = (Block *)b;
98 void *Allocator::alloc1()
101 = (SegmentHeader *)::operator new(sizeof(SegmentHeader)
102 + ((objectSize_ + sizeof(BlockHeader))
103 * blocksPerSegment_));
104 seg->next = segments_;
107 seg->freeList = &freeList_;
108 char *p = (char *)(seg + 1);
110 for (size_t n = blocksPerSegment_; n > 0; n--) {
111 ((Block *)p)->next = head;
112 ((Block *)p)->header.seg = seg;
114 p += sizeof(BlockHeader) + objectSize_;
116 freeList_ = head->next;
117 return &(head->next);
120 void Allocator::tooBig(size_t sz)
122 ASSERT(sz <= objectSize_);