1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*-------------------------------------------------------------------------
15  *
16  * Created:     H5HLint.c
17  *              Oct 12 2008
18  *              Quincey Koziol <koziol@hdfgroup.org>
19  *
20  * Purpose:     Local heap internal routines.
21  *
22  *-------------------------------------------------------------------------
23  */
24 
25 /****************/
26 /* Module Setup */
27 /****************/
28 
29 #include "H5HLmodule.h"         /* This source code file is part of the H5HL module */
30 
31 
32 /***********/
33 /* Headers */
34 /***********/
35 #include "H5private.h"      /* Generic Functions            */
36 #include "H5Eprivate.h"     /* Error handling               */
37 #include "H5FLprivate.h"    /* Free lists                   */
38 #include "H5HLpkg.h"        /* Local Heaps                  */
39 
40 
41 /****************/
42 /* Local Macros */
43 /****************/
44 
45 
46 /******************/
47 /* Local Typedefs */
48 /******************/
49 
50 
51 /********************/
52 /* Package Typedefs */
53 /********************/
54 
55 
56 /********************/
57 /* Local Prototypes */
58 /********************/
59 
60 
61 /*********************/
62 /* Package Variables */
63 /*********************/
64 
65 
66 /*****************************/
67 /* Library Private Variables */
68 /*****************************/
69 
70 
71 /*******************/
72 /* Local Variables */
73 /*******************/
74 
75 /* Declare a free list to manage the H5HL_t struct */
76 H5FL_DEFINE_STATIC(H5HL_t);
77 
78 
79 
80 /*-------------------------------------------------------------------------
81  * Function:    H5HL__new
82  *
83  * Purpose:     Create a new local heap object
84  *
85  * Return:      Success:    non-NULL pointer to new local heap
86  *              Failure:    NULL
87  *
88  * Programmer:  Quincey Koziol
89  *              Jan  5 2010
90  *
91  *-------------------------------------------------------------------------
92  */
93 BEGIN_FUNC(PKG, ERR,
94 H5HL_t *, NULL, NULL,
95 H5HL__new(size_t sizeof_size, size_t sizeof_addr, size_t prfx_size))
96 
97     H5HL_t *heap = NULL;        /* New local heap */
98 
99     /* check arguments */
100     HDassert(sizeof_size > 0);
101     HDassert(sizeof_addr > 0);
102     HDassert(prfx_size > 0);
103 
104     /* Allocate new local heap structure */
105     if(NULL == (heap = H5FL_CALLOC(H5HL_t)))
106         H5E_THROW(H5E_CANTALLOC, "memory allocation failed");
107 
108     /* Initialize non-zero fields */
109     heap->sizeof_size = sizeof_size;
110     heap->sizeof_addr = sizeof_addr;
111     heap->prfx_size = prfx_size;
112 
113     /* Set the return value */
114     ret_value = heap;
115 
116 CATCH
117     if(!ret_value && heap != NULL)
118         if (NULL == (heap = H5FL_FREE(H5HL_t, heap)))
119             H5E_THROW(H5E_CANTFREE, "can't free heap memory");
120 
121 END_FUNC(PKG) /* end H5HL__new() */
122 
123 
124 /*-------------------------------------------------------------------------
125  * Function:	H5HL__inc_rc
126  *
127  * Purpose:     Increment ref. count on heap
128  *
129  * Return:      SUCCEED (Can't fail)
130  *
131  * Programmer:  Quincey Koziol
132  *              Oct 12 2008
133  *
134  *-------------------------------------------------------------------------
135  */
136 BEGIN_FUNC(PKG, NOERR,
137 herr_t, SUCCEED, -,
138 H5HL__inc_rc(H5HL_t *heap))
139 
140     /* check arguments */
141     HDassert(heap);
142 
143     /* Increment heap's ref. count */
144     heap->rc++;
145 
146 END_FUNC(PKG) /* end H5HL__inc_rc() */
147 
148 
149 /*-------------------------------------------------------------------------
150  * Function:	H5HL__dec_rc
151  *
152  * Purpose:     Decrement ref. count on heap
153  *
154  * Return:      SUCCEED/FAIL
155  *
156  * Programmer:  Quincey Koziol
157  *              Oct 12 2008
158  *
159  *-------------------------------------------------------------------------
160  */
161 BEGIN_FUNC(PKG, ERR,
162 herr_t, SUCCEED, FAIL,
163 H5HL__dec_rc(H5HL_t *heap))
164 
165     /* check arguments */
166     HDassert(heap);
167 
168     /* Decrement heap's ref. count */
169     heap->rc--;
170 
171 CATCH
172     /* Check if we should destroy the heap */
173     if(heap->rc == 0 && FAIL == H5HL__dest(heap))
174         H5E_THROW(H5E_CANTFREE, "unable to destroy local heap");
175 
176 END_FUNC(PKG) /* end H5HL__dec_rc() */
177 
178 
179 /*-------------------------------------------------------------------------
180  * Function:    H5HL__dest
181  *
182  * Purpose:     Destroys a heap in memory.
183  *
184  * Return:      SUCCEED/FAIL
185  *
186  * Programmer:  Quincey Koziol
187  *              Jan 15 2003
188  *
189  *-------------------------------------------------------------------------
190  */
191 BEGIN_FUNC(PKG, ERR,
192 herr_t, SUCCEED, FAIL,
193 H5HL__dest(H5HL_t *heap))
194 
195     /* check arguments */
196     HDassert(heap);
197 
198     /* Verify that node is unused */
199     HDassert(heap->prots == 0);
200     HDassert(heap->rc == 0);
201     HDassert(heap->prfx == NULL);
202     HDassert(heap->dblk == NULL);
203 
204 CATCH
205     if(heap->dblk_image)
206         if(NULL != (heap->dblk_image = H5FL_BLK_FREE(lheap_chunk, heap->dblk_image)))
207             H5E_THROW(H5E_CANTFREE, "unable to free local heap data block image");
208     while(heap->freelist) {
209         H5HL_free_t	*fl;
210 
211         fl = heap->freelist;
212         heap->freelist = fl->next;
213         if(NULL != (fl = H5FL_FREE(H5HL_free_t, fl)))
214             H5E_THROW(H5E_CANTFREE, "unable to free local heap free list");
215     } /* end while */
216 
217     if(NULL != (heap = H5FL_FREE(H5HL_t, heap)))
218         H5E_THROW(H5E_CANTFREE, "unable to free local heap");
219 
220 END_FUNC(PKG) /* end H5HL__dest() */
221