"Fossies" - the Fresh Open Source Software Archive 
Member "statist-1.4.2/src/memory_handling.c" (31 Aug 2005, 2461 Bytes) of package /linux/privat/old/statist-1.4.2.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "memory_handling.c" see the
Fossies "Dox" file reference documentation.
1 /* This file is part of statist
2 **
3 ** It is distributed under the GNU General Public License.
4 ** See the file COPYING for details.
5 **
6 ** (c) 1997 Dirk Melcher old email: Dirk.Melcher@usf.Uni-Osnabrueck.DE
7 ** (c) 2001 Bernhard Reiter
8 **
9 ** published by Bernhard Reiter http://www.usf.Uni-Osnabrueck.DE/~breiter
10 ** $Id: memory_handling.c,v 1.3 2005/08/31 12:48:16 jakson Exp $
11 ***************************************************************/
12
13
14 /* Memory handling functions
15 *
16 * */
17
18 #include <stdlib.h>
19 #include "statist.h"
20
21 #include "memory_handling.h"
22
23 typedef struct {
24 size_t size;
25 void * previous_chunk;
26 void * memory;
27 } memory_chunk ;
28
29 /* pointer to the last memory chunk */
30 static memory_chunk *ptr_memory_chunks=NULL;
31
32
33
34 void *myrealloc(void * pointer, int newsize) {
35 /* realloc() with error check */
36 pointer = realloc(pointer, newsize);
37 if (pointer == NULL) {
38 out_err(FAT, ERR_FILE, ERR_LINE,
39 _("Not enough memory. Terminating!") );
40 finish();
41 exit(1);
42 }
43 return pointer;
44 }
45
46 void *mycalloc(int nitems, int size) {
47 /* calloc() with error check */
48 void *pointer;
49 pointer = calloc(nitems, size);
50 if (pointer == NULL) {
51 out_err(FAT, ERR_FILE, ERR_LINE,
52 _("Not enough memory. Terminating!") );
53 finish();
54 exit(1);
55 }
56 return pointer;
57 }
58
59
60 void *mymalloc(int size) {
61 /* malloc() with error check */
62 void *pointer;
63 pointer = malloc(size);
64 if (pointer == NULL) {
65 out_err(FAT, ERR_FILE, ERR_LINE,
66 _("Not enough memory. Terminating!") );
67 finish();
68 exit(1);
69 }
70 return pointer;
71 }
72
73 void myfree(void *ptr) {
74 /* pair function to mycalloc() and mymalloc() */
75 free(ptr);
76 }
77
78
79 void *m_calloc(int nitems, int size) {
80 /* calloc() should be used for temp variables,
81 * keeps a record of allocated blocks
82 * you need to call m_freeall to free them all
83 */
84 memory_chunk *chunk;
85 chunk = mycalloc(1,sizeof(memory_chunk));
86
87 /* get mem and fill struct */
88 chunk->memory = mycalloc(nitems, size);
89 chunk->size = nitems * size;
90 chunk->previous_chunk=ptr_memory_chunks;
91 /* put entry pointer to last chunk */
92 ptr_memory_chunks=chunk;
93
94 return chunk->memory;
95 }
96
97
98 int m_freeall(void) {
99 /* frees all blocks allocated by several calls to m_calloc() */
100 memory_chunk *chunk=NULL;
101
102 while(ptr_memory_chunks!=NULL) {
103 chunk=ptr_memory_chunks;
104
105 myfree(chunk->memory);
106 ptr_memory_chunks=chunk->previous_chunk;
107
108 /* free struct */
109 myfree(chunk);
110 }
111
112 return 0;
113 }
114