summaryrefslogtreecommitdiff
path: root/libsn/sn-util.h
blob: 4da3c1e32bbe21b29f0f94da3d9276273d15a47c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* 
 * Copyright (C) 2002 Red Hat, Inc.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */


#ifndef __LF_UTIL_H__
#define __LF_UTIL_H__

/* Guard C code in headers, while including them from C++ */
#ifdef  __cplusplus
# define LF_BEGIN_DECLS  extern "C" {
# define LF_END_DECLS    }
#else
# define LF_BEGIN_DECLS
# define LF_END_DECLS
#endif

LF_BEGIN_DECLS

typedef unsigned long lf_size_t;
typedef int           lf_bool_t;

/* Padding in vtables */
typedef void (* LfPaddingFunc) (void);
/* Free data */
typedef void (* LfFreeFunc)    (void *data);

void* lf_malloc      (lf_size_t  n_bytes);
void* lf_malloc0     (lf_size_t  n_bytes);
void* lf_realloc     (void      *mem,
                      lf_size_t  n_bytes);
void  lf_free        (void      *mem);
void* lf_try_malloc  (lf_size_t  n_bytes);
void* lf_try_realloc (void      *mem,
                      lf_size_t  n_bytes);

/* Convenience memory allocators
 */
#define lf_new(struct_type, n_structs)		\
    ((struct_type *) lf_malloc (((lf_size_t) sizeof (struct_type)) * ((lf_size_t) (n_structs))))
#define lf_new0(struct_type, n_structs)		\
    ((struct_type *) lf_malloc0 (((lf_size_t) sizeof (struct_type)) * ((lf_size_t) (n_structs))))
#define lf_renew(struct_type, mem, n_structs)	\
    ((struct_type *) lf_realloc ((mem), ((lf_size_t) sizeof (struct_type)) * ((lf_size_t) (n_structs))))


/* Memory allocation virtualization, so you can override memory
 * allocation behavior.  lf_mem_set_vtable() has to be the very first
 * liblf function called if being used
 */
typedef struct
{
  void* (*malloc)      (lf_size_t    n_bytes);
  void* (*realloc)     (void        *mem,
                        lf_size_t    n_bytes);
  void  (*free)        (void        *mem);
  /* optional */
  void* (*calloc)      (lf_size_t    n_blocks,
                        lf_size_t    n_block_bytes);
  void* (*try_malloc)  (lf_size_t    n_bytes);
  void* (*try_realloc) (void        *mem,
                        lf_size_t    n_bytes);
  LfPaddingFunc        padding1;
  LfPaddingFunc        padding2;
} LfMemVTable;

void      lf_mem_set_vtable       (LfMemVTable *vtable);
lf_bool_t lf_mem_is_system_malloc (void);

typedef lf_bool_t (* LfUtf8ValidateFunc) (const char *str,
                                          int         max_len);

void lf_set_utf8_validator (LfUtf8ValidateFunc validate_func);

LF_END_DECLS

#endif /* __LF_UTIL_H__ */