github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/runtime/mfixalloc.c (about) 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. 4 5 // Fixed-size object allocator. Returned memory is not zeroed. 6 // 7 // See malloc.h for overview. 8 9 #include "runtime.h" 10 #include "arch.h" 11 #include "malloc.h" 12 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) 17 { 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; 26 } 27 28 void* 29 runtime_FixAlloc_Alloc(FixAlloc *f) 30 { 31 void *v; 32 33 if(f->size == 0) { 34 runtime_printf("runtime: use of FixAlloc_Alloc before FixAlloc_Init\n"); 35 runtime_throw("runtime: internal error"); 36 } 37 38 if(f->list) { 39 v = f->list; 40 f->list = *(void**)f->list; 41 f->inuse += f->size; 42 return v; 43 } 44 if(f->nchunk < f->size) { 45 f->chunk = runtime_persistentalloc(FixAllocChunk, 0, f->stat); 46 f->nchunk = FixAllocChunk; 47 } 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; 55 } 56 57 void 58 runtime_FixAlloc_Free(FixAlloc *f, void *p) 59 { 60 f->inuse -= f->size; 61 *(void**)p = f->list; 62 f->list = p; 63 } 64