3 * Copyright (C) 2010 PCB Contributors (see ChangeLog for details)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * Contact addresses for paper mail and Email:
20 * harry eaton, 6697 Buttonhole Ct, Columbia, MD 21044 USA
21 * haceaton@aplcomm.jhuapl.edu
28 /* we need one ID per context - short int with 64k IDs should be enough */
29 typedef unsigned int leaky_idx_t
;
32 /* This structure should be as big as void *, which should be the natural
33 bit-width of the architecture. We allocate extra admin space to be as big
34 as this union, to preserve alignment of pointers returned by malloc().
35 NOTE: in the special corner case when leaky_idx_t is wider than void * but
36 not multiple of it, the alignment will be messed up, potentially causing slower
43 static void **free_list
= NULL
;
44 static leaky_idx_t free_size
= 0;
47 void *leaky_malloc (size_t size
)
49 void *new_memory
= malloc(size
+ sizeof(leaky_admin_t
));
51 free_list
= (void **)realloc (free_list
, (free_size
+ 1) * sizeof(void *));
52 free_list
[free_size
] = new_memory
;
53 *(leaky_idx_t
*)new_memory
= free_size
;
56 return new_memory
+ sizeof(leaky_admin_t
);
59 void *leaky_calloc (size_t nmemb
, size_t size
)
61 size_t size_
= size
* nmemb
;
62 void *new_memory
= leaky_malloc (size_
);
64 memset (new_memory
, 0, size_
);
68 void *leaky_realloc (void* old_memory
, size_t size
)
73 if (old_memory
== NULL
)
74 return leaky_malloc (size
);
76 old_memory
-= sizeof(leaky_admin_t
);
78 i
= *(leaky_idx_t
*)old_memory
;
80 new_memory
= realloc (old_memory
, size
+ sizeof(leaky_admin_t
));
81 free_list
[i
] = new_memory
;
83 return new_memory
+ sizeof(leaky_admin_t
);
86 void leaky_uninit (void)
90 for (i
= 0; i
< free_size
; i
++)
97 void leaky_init (void)