| 1 |
/* Abstract map data type, with out-of-memory checking. |
| 2 |
Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 3 |
Written by Bruno Haible <[email protected]>, 2018. |
| 4 |
|
| 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 3 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 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. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
#ifndef _GL_XMAP_H |
| 19 |
#define _GL_XMAP_H |
| 20 |
|
| 21 |
/* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, |
| 22 |
_GL_ATTRIBUTE_RETURNS_NONNULL. */ |
| 23 |
#if !_GL_CONFIG_H_INCLUDED |
| 24 |
#error "Please include config.h first." |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "gl_map.h" |
| 28 |
#include "xalloc.h" |
| 29 |
|
| 30 |
_GL_INLINE_HEADER_BEGIN |
| 31 |
#ifndef GL_XMAP_INLINE |
| 32 |
# define GL_XMAP_INLINE _GL_INLINE |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
#ifdef __cplusplus |
| 37 |
extern "C" { |
| 38 |
#endif |
| 39 |
|
| 40 |
/* These functions are thin wrappers around the corresponding functions with |
| 41 |
_nx_ infix from gl_map.h. Upon out-of-memory, they invoke xalloc_die (), |
| 42 |
instead of returning an error indicator. */ |
| 43 |
#if 0 /* These are defined inline below. */ |
| 44 |
extern gl_map_t gl_map_create_empty (gl_map_implementation_t implementation, |
| 45 |
gl_mapkey_equals_fn equals_fn, |
| 46 |
gl_mapkey_hashcode_fn hashcode_fn, |
| 47 |
gl_mapkey_dispose_fn kdispose_fn, |
| 48 |
gl_mapvalue_dispose_fn vdispose_fn) |
| 49 |
/*_GL_ATTRIBUTE_DEALLOC (gl_map_free, 1)*/ |
| 50 |
_GL_ATTRIBUTE_RETURNS_NONNULL; |
| 51 |
extern bool gl_map_put (gl_map_t map, const void *key, const void *value); |
| 52 |
extern bool gl_map_getput (gl_map_t map, const void *key, const void *value, |
| 53 |
const void **oldvaluep); |
| 54 |
#endif |
| 55 |
|
| 56 |
GL_XMAP_INLINE |
| 57 |
/*_GL_ATTRIBUTE_DEALLOC (gl_map_free, 1)*/ |
| 58 |
_GL_ATTRIBUTE_RETURNS_NONNULL |
| 59 |
gl_map_t |
| 60 |
gl_map_create_empty (gl_map_implementation_t implementation, |
| 61 |
gl_mapkey_equals_fn equals_fn, |
| 62 |
gl_mapkey_hashcode_fn hashcode_fn, |
| 63 |
gl_mapkey_dispose_fn kdispose_fn, |
| 64 |
gl_mapvalue_dispose_fn vdispose_fn) |
| 65 |
{ |
| 66 |
gl_map_t result = |
| 67 |
gl_map_nx_create_empty (implementation, equals_fn, hashcode_fn, |
| 68 |
kdispose_fn, vdispose_fn); |
| 69 |
if (result == NULL) |
| 70 |
xalloc_die (); |
| 71 |
return result; |
| 72 |
} |
| 73 |
|
| 74 |
GL_XMAP_INLINE bool |
| 75 |
gl_map_put (gl_map_t map, const void *key, const void *value) |
| 76 |
{ |
| 77 |
int result = gl_map_nx_put (map, key, value); |
| 78 |
if (result < 0) |
| 79 |
xalloc_die (); |
| 80 |
return result; |
| 81 |
} |
| 82 |
|
| 83 |
GL_XMAP_INLINE bool |
| 84 |
gl_map_getput (gl_map_t map, const void *key, const void *value, |
| 85 |
const void **oldvaluep) |
| 86 |
{ |
| 87 |
int result = gl_map_nx_getput (map, key, value, oldvaluep); |
| 88 |
if (result < 0) |
| 89 |
xalloc_die (); |
| 90 |
return result; |
| 91 |
} |
| 92 |
|
| 93 |
#ifdef __cplusplus |
| 94 |
} |
| 95 |
#endif |
| 96 |
|
| 97 |
_GL_INLINE_HEADER_END |
| 98 |
|
| 99 |
#endif /* _GL_XMAP_H */ |