Implement a flag -fext-numeric-literals that allows control of whether GNU
[official-gcc.git] / libgo / runtime / mfixalloc.c
blob109cfe8eeaa91768fa3fdf7dc449ec500427a8a0
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Fixed-size object allocator. Returned memory is not zeroed.
6 //
7 // See malloc.h for overview.
9 #include "runtime.h"
10 #include "arch.h"
11 #include "malloc.h"
13 // Initialize f to allocate objects of the given size,
14 // using the allocator to obtain chunks of memory.
15 void
16 runtime_FixAlloc_Init(FixAlloc *f, uintptr size, void *(*alloc)(uintptr), void (*first)(void*, byte*), void *arg)
18 f->size = size;
19 f->alloc = alloc;
20 f->first = first;
21 f->arg = arg;
22 f->list = nil;
23 f->chunk = nil;
24 f->nchunk = 0;
25 f->inuse = 0;
26 f->sys = 0;
29 void*
30 runtime_FixAlloc_Alloc(FixAlloc *f)
32 void *v;
34 if(f->list) {
35 v = f->list;
36 f->list = *(void**)f->list;
37 f->inuse += f->size;
38 return v;
40 if(f->nchunk < f->size) {
41 f->sys += FixAllocChunk;
42 f->chunk = f->alloc(FixAllocChunk);
43 if(f->chunk == nil)
44 runtime_throw("out of memory (FixAlloc)");
45 f->nchunk = FixAllocChunk;
47 v = f->chunk;
48 if(f->first)
49 f->first(f->arg, v);
50 f->chunk += f->size;
51 f->nchunk -= f->size;
52 f->inuse += f->size;
53 return v;
56 void
57 runtime_FixAlloc_Free(FixAlloc *f, void *p)
59 f->inuse -= f->size;
60 *(void**)p = f->list;
61 f->list = p;