Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 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" |
Dmitriy Vyukov | c14b268 | 2011-10-06 18:42:51 +0300 | [diff] [blame] | 10 | #include "arch.h" |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 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 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 16 | runtime·FixAlloc_Init(FixAlloc *f, uintptr size, void *(*alloc)(uintptr), void (*first)(void*, byte*), void *arg) |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 17 | { |
| 18 | f->size = size; |
| 19 | f->alloc = alloc; |
Russ Cox | 9f726c2 | 2009-01-28 15:22:16 -0800 | [diff] [blame] | 20 | f->first = first; |
| 21 | f->arg = arg; |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 22 | f->list = nil; |
| 23 | f->chunk = nil; |
| 24 | f->nchunk = 0; |
Russ Cox | a709876 | 2010-03-29 13:06:26 -0700 | [diff] [blame] | 25 | f->inuse = 0; |
| 26 | f->sys = 0; |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void* |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 30 | runtime·FixAlloc_Alloc(FixAlloc *f) |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 31 | { |
| 32 | void *v; |
| 33 | |
| 34 | if(f->list) { |
| 35 | v = f->list; |
| 36 | f->list = *(void**)f->list; |
Russ Cox | a709876 | 2010-03-29 13:06:26 -0700 | [diff] [blame] | 37 | f->inuse += f->size; |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 38 | return v; |
| 39 | } |
| 40 | if(f->nchunk < f->size) { |
Russ Cox | a709876 | 2010-03-29 13:06:26 -0700 | [diff] [blame] | 41 | f->sys += FixAllocChunk; |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 42 | f->chunk = f->alloc(FixAllocChunk); |
| 43 | if(f->chunk == nil) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 44 | runtime·throw("out of memory (FixAlloc)"); |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 45 | f->nchunk = FixAllocChunk; |
| 46 | } |
| 47 | v = f->chunk; |
Russ Cox | 9f726c2 | 2009-01-28 15:22:16 -0800 | [diff] [blame] | 48 | if(f->first) |
| 49 | f->first(f->arg, v); |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 50 | f->chunk += f->size; |
| 51 | f->nchunk -= f->size; |
Russ Cox | a709876 | 2010-03-29 13:06:26 -0700 | [diff] [blame] | 52 | f->inuse += f->size; |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 53 | return v; |
| 54 | } |
| 55 | |
| 56 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 57 | runtime·FixAlloc_Free(FixAlloc *f, void *p) |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 58 | { |
Russ Cox | a709876 | 2010-03-29 13:06:26 -0700 | [diff] [blame] | 59 | f->inuse -= f->size; |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 60 | *(void**)p = f->list; |
| 61 | f->list = p; |
| 62 | } |
| 63 | |