1 /* 2 * Copyright (C) 2002 Red Hat, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person 5 * obtaining a copy of this software and associated documentation 6 * files (the "Software"), to deal in the Software without 7 * restriction, including without limitation the rights to use, copy, 8 * modify, merge, publish, distribute, sublicense, and/or sell copies 9 * of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 25 26 #ifndef __SN_UTIL_H__ 27 #define __SN_UTIL_H__ 28 29 /* Guard C code in headers, while including them from C++ */ 30 #ifdef __cplusplus 31 # define SN_BEGIN_DECLS extern "C" { 32 # define SN_END_DECLS } 33 #else 34 # define SN_BEGIN_DECLS 35 # define SN_END_DECLS 36 #endif 37 38 SN_BEGIN_DECLS 39 40 typedef unsigned long sn_size_t; 41 typedef int sn_bool_t; 42 43 /* Padding in vtables */ 44 typedef void (* SnPaddingFunc) (void); 45 /* Free data */ 46 typedef void (* SnFreeFunc) (void *data); 47 48 void* sn_malloc (sn_size_t n_bytes); 49 void* sn_malloc0 (sn_size_t n_bytes); 50 void* sn_realloc (void *mem, 51 sn_size_t n_bytes); 52 void sn_free (void *mem); 53 void* sn_try_malloc (sn_size_t n_bytes); 54 void* sn_try_realloc (void *mem, 55 sn_size_t n_bytes); 56 57 /* Convenience memory allocators 58 */ 59 #define sn_new(struct_type, n_structs) \ 60 ((struct_type *) sn_malloc (((sn_size_t) sizeof (struct_type)) * ((sn_size_t) (n_structs)))) 61 #define sn_new0(struct_type, n_structs) \ 62 ((struct_type *) sn_malloc0 (((sn_size_t) sizeof (struct_type)) * ((sn_size_t) (n_structs)))) 63 #define sn_renew(struct_type, mem, n_structs) \ 64 ((struct_type *) sn_realloc ((mem), ((sn_size_t) sizeof (struct_type)) * ((sn_size_t) (n_structs)))) 65 66 67 /* Memory allocation virtualization, so you can override memory 68 * allocation behavior. sn_mem_set_vtable() has to be the very first 69 * libsn function called if being used 70 */ 71 typedef struct 72 { 73 void* (*malloc) (sn_size_t n_bytes); 74 void* (*realloc) (void *mem, 75 sn_size_t n_bytes); 76 void (*free) (void *mem); 77 /* optional */ 78 void* (*calloc) (sn_size_t n_blocks, 79 sn_size_t n_block_bytes); 80 void* (*try_malloc) (sn_size_t n_bytes); 81 void* (*try_realloc) (void *mem, 82 sn_size_t n_bytes); 83 SnPaddingFunc padding1; 84 SnPaddingFunc padding2; 85 } SnMemVTable; 86 87 void sn_mem_set_vtable (SnMemVTable *vtable); 88 sn_bool_t sn_mem_is_system_malloc (void); 89 90 typedef sn_bool_t (* SnUtf8ValidateFunc) (const char *str, 91 int max_len); 92 93 void sn_set_utf8_validator (SnUtf8ValidateFunc validate_func); 94 95 SN_END_DECLS 96 97 #endif /* __SN_UTIL_H__ */ 98