Roll back to using static MACHINE.SID after consultation with Andrew. This
[kai/samba.git] / source3 / lib / talloc.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    Samba temporary memory allocation functions
5    Copyright (C) Andrew Tridgell 2000
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /* this is a very simple temporary memory allocator. To use it do the following:
23
24    1) when you first want to allocate a pool of meomry use
25    talloc_init() and save the resulting context pointer somewhere
26
27    2) to allocate memory use talloc()
28
29    3) when _all_ of the memory allocated using this context is no longer needed
30    use talloc_destroy()
31
32    talloc does not zero the memory. It guarantees memory of a
33    TALLOC_ALIGN alignment
34 */
35
36 #include "includes.h"
37
38 #define TALLOC_ALIGN 32
39 #define TALLOC_CHUNK_SIZE (0x2000)
40
41 /* initialissa talloc context. */
42 TALLOC_CTX *talloc_init(void)
43 {
44         TALLOC_CTX *t;
45
46         t = (TALLOC_CTX *)malloc(sizeof(*t));
47         if (!t) return NULL;
48
49         t->list = NULL;
50
51         return t;
52 }
53
54 /* allocate a bit of memory from the specified pool */
55 void *talloc(TALLOC_CTX *t, size_t size)
56 {
57         void *p;
58
59         size = (size + TALLOC_ALIGN) & (~TALLOC_ALIGN-1);
60
61         if (!t->list || (t->list->total_size - t->list->alloc_size) < size) {
62                 struct talloc_chunk *c;
63                 size_t asize = (size + TALLOC_CHUNK_SIZE) & ~(TALLOC_CHUNK_SIZE-1);
64
65                 c = (struct talloc_chunk *)malloc(sizeof(*c));
66                 if (!c) return NULL;
67                 c->next = t->list;
68                 c->ptr = (void *)malloc(asize);
69                 if (!c->ptr) {
70                         free(c);
71                         return NULL;
72                 }
73                 c->alloc_size = 0;
74                 c->total_size = asize;
75                 t->list = c;
76         }
77
78         p = ((char *)t->list->ptr) + t->list->alloc_size;
79         t->list->alloc_size += size;
80         return p;
81 }
82
83 /* destroy a whole pool */
84 void talloc_destroy(TALLOC_CTX *t)
85 {
86         struct talloc_chunk *c;
87         
88         while (t->list) {
89                 c = t->list->next;
90                 free(t->list->ptr);
91                 free(t->list);
92                 t->list = c;
93         }
94
95         free(t);
96 }