1 /*
2 Copyright (C) 2003 Cedric Cellier, Dominique Lavault
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18 /*
19  * Manage a memory area of determined size
20  *
21  * Some programs can make use of memory if available to raise performences.
22  * Then, the user must provide a memory size that the program is allowed to
23  * use.
24  * Some functions can then tell what's the memory consuption of the program.
25  *
26  * Of course, there is only one spool available per process.
27  *
28  * Some functions can register adresses : that is, they wan't the bloc which
29  * countain that adress, if any, to stau in memory even when the creator of the bloc will
30  * unregister it.
31  *
32  */
33 
34 #ifndef _GLTV_MEMSPOOL_H_
35 #define _GLTV_MEMSPOOL_H_
36 #include <stdlib.h>
37 /* Must be called at the begining or after gltv_memspool_end(),
38  * with the size in bytes of the spool */
39 extern int gltv_memspool_init(unsigned new_size);
40 /* Must be called after all alloced blocks have been freed */
41 extern void gltv_memspool_end();
42 /* To get some bytes from the spool.
43  * Return 0 if malloc fails.
44  * This is not an error when the declared size of the spool is exceeded. */
45 extern void *gltv_memspool_alloc_d(unsigned requested_size, char const *, size_t);
46 #define gltv_memspool_alloc(size) gltv_memspool_alloc_d(size, __FILE__, __LINE__)
47 /* resize the allocated block */
48 /* like the system call, this function returns the new adress of the block, wich
49  * datas are unchanged, or 0 if the realloc fails, in wich case the farmer block
50  * still exists */
51 extern void *gltv_memspool_realloc(void *mem, unsigned new_size);
52 /* Unregister the memory block so that it can be freed */
53 extern void gltv_memspool_unregister(void *mem);
54 /* register the memory block countaining the adress given so that it won't get freed until we unregister from it
55  * return 0 if no blocs countained that adress */
56 extern int gltv_memspool_register(void *mem);
57 /* To know how many percents of the memory is used */
58 extern int gltv_memspool_consumption();
59 /* return the size of a previously allocated block */
60 extern unsigned gltv_memspool_blocksize(void *mem);
61 /* stupid thing to check all headers are valid */
62 extern void gltv_memspool_test();
63 
64 #endif
65