1 /*
2                                   NETWIB
3                              Network library
4                 Copyright(c) 1999-2010 Laurent Constantin
5                                   -----
6 
7   Main server   : http://www.laurentconstantin.com/
8   Backup server : http://laurentconstantin.free.fr/
9   [my current email address is on the web servers]
10 
11                                   -----
12   This file is part of Netwib.
13 
14   Netwib is free software: you can redistribute it and/or modify
15   it under the terms of the GNU General Public License version 3
16   as published by the Free Software Foundation.
17 
18   Netwib is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21   GNU General Public License for more details (http://www.gnu.org/).
22 
23 ------------------------------------------------------------------------
24 */
25 
26 #include <netwib/inc/maininc.h>
27 
28 /*-------------------------------------------------------------*/
netwib_ptr_malloc(netwib_uint32 allocsize,netwib_ptr * pptr)29 netwib_err netwib_ptr_malloc(netwib_uint32 allocsize,
30                              netwib_ptr *pptr)
31 {
32   netwib_ptr ptr;
33 
34 #if NETWIB_DEBUG_MEM_CORRUPT==0
35   ptr = (netwib_ptr) malloc(allocsize);
36   if (ptr == NULL)
37     return(NETWIB_ERR_FUMALLOC);
38 #else
39   netwib_er(netwib_debug_memcorrupt_alloc(allocsize, &ptr));
40   netwib_er(netwib_debug_memcorrupt_check_periodic());
41 #endif
42 
43 #if NETWIB_DEBUG_LEAK!=0
44   netwib_er(netwib_debug_leak_add_mem(allocsize, ptr));
45 #endif
46 
47   if (pptr != NULL) {
48     *pptr = ptr;
49   } else {
50     /* our check to see if memory can really be allocated succeeded */
51 #if NETWIB_DEBUG_LEAK!=0
52     netwib_er(netwib_debug_leak_del_mem(ptr));
53 #endif
54 
55 #if NETWIB_DEBUG_MEM_CORRUPT==0
56     free(ptr);
57 #else
58     netwib_er(netwib_debug_memcorrupt_free(&ptr));
59     netwib_er(netwib_debug_memcorrupt_check_periodic());
60 #endif
61   }
62 
63   return(NETWIB_ERR_OK);
64 }
65 
66 /*-------------------------------------------------------------*/
netwib_ptr_realloc(netwib_uint32 newallocsize,netwib_ptr * pptr)67 netwib_err netwib_ptr_realloc(netwib_uint32 newallocsize,
68                               netwib_ptr *pptr)
69 {
70   if (pptr == NULL) {
71     /* we have no sufficient info, but we suppose it will be ok if
72        a correct parameter is given*/
73     return(NETWIB_ERR_OK);
74   }
75 
76 #if NETWIB_DEBUG_LEAK!=0
77   netwib_er(netwib_debug_leak_del_mem(*pptr));
78 #endif
79 
80 #if NETWIB_DEBUG_MEM_CORRUPT==0
81   *pptr = (netwib_ptr) realloc(*pptr, newallocsize);
82   if (*pptr == NULL)
83     return(NETWIB_ERR_FUREALLOC);
84 #else
85   netwib_er(netwib_debug_memcorrupt_realloc(newallocsize, pptr));
86   netwib_er(netwib_debug_memcorrupt_check_periodic());
87 #endif
88 
89 #if NETWIB_DEBUG_LEAK!=0
90   netwib_er(netwib_debug_leak_add_mem(newallocsize, *pptr));
91 #endif
92 
93   return(NETWIB_ERR_OK);
94 }
95 
96 /*-------------------------------------------------------------*/
netwib_ptr_free(netwib_ptr * pptr)97 netwib_err netwib_ptr_free(netwib_ptr *pptr)
98 {
99   if (pptr == NULL) {
100     /* we have no sufficient info, but we suppose it will be ok if
101        a correct parameter is given*/
102     return(NETWIB_ERR_OK);
103   }
104   if (*pptr == NULL)
105     return(NETWIB_ERR_LOOBJCLOSEALREADYCLOSED);
106 
107 #if NETWIB_DEBUG_LEAK!=0
108   netwib_er(netwib_debug_leak_del_mem(*pptr));
109 #endif
110 
111 #if NETWIB_DEBUG_MEM_CORRUPT==0
112   free(*pptr);
113 #else
114   netwib_er(netwib_debug_memcorrupt_free(pptr));
115   netwib_er(netwib_debug_memcorrupt_check_periodic());
116 #endif
117 
118   *pptr = NULL;
119   return(NETWIB_ERR_OK);
120 }
121