PR target/60017
[official-gcc.git] / libgo / runtime / mfixalloc.c
blob9d0b3bbda7e8f9c7adfe43681464c513d42d2fdb
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 (*first)(void*, byte*), void *arg, uint64 *stat)
18 f->size = size;
19 f->first = first;
20 f->arg = arg;
21 f->list = nil;
22 f->chunk = nil;
23 f->nchunk = 0;
24 f->inuse = 0;
25 f->stat = stat;
28 void*
29 runtime_FixAlloc_Alloc(FixAlloc *f)
31 void *v;
33 if(f->size == 0) {
34 runtime_printf("runtime: use of FixAlloc_Alloc before FixAlloc_Init\n");
35 runtime_throw("runtime: internal error");
38 if(f->list) {
39 v = f->list;
40 f->list = *(void**)f->list;
41 f->inuse += f->size;
42 return v;
44 if(f->nchunk < f->size) {
45 f->chunk = runtime_persistentalloc(FixAllocChunk, 0, f->stat);
46 f->nchunk = FixAllocChunk;
48 v = f->chunk;
49 if(f->first)
50 f->first(f->arg, v);
51 f->chunk += f->size;
52 f->nchunk -= f->size;
53 f->inuse += f->size;
54 return v;
57 void
58 runtime_FixAlloc_Free(FixAlloc *f, void *p)
60 f->inuse -= f->size;
61 *(void**)p = f->list;
62 f->list = p;