su  1.13.17
su_alloc.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2006 Nokia Corporation.
5  *
6  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #ifndef SU_ALLOC_H
26 #define SU_ALLOC_H
27 
37 #ifndef SU_TYPES_H
38 #include <sofia-sip/su_types.h>
39 #endif
40 
41 #include <stdarg.h>
42 
43 SOFIA_BEGIN_DECLS
44 
45 #ifndef SU_HOME_T
46 #define SU_HOME_T struct su_home_s
47 #endif
48 
50 typedef SU_HOME_T su_home_t;
51 typedef struct su_block_s su_block_t;
52 
54 typedef struct su_alock su_alock_t;
55 
57 struct su_home_s {
58  int suh_size;
59  su_block_t *suh_blocks;
60  su_alock_t *suh_lock;
61 };
62 
63 #define SU_HOME_INIT(obj) { 0, NULL, NULL }
64 
65 SU_DLL void *su_home_new(isize_t size)
66  __attribute__((__malloc__));
67 
68 #if (defined(HAVE_MEMLEAK_LOG) && (HAVE_MEMLEAK_LOG != 1))
69 
70 int _su_home_mutex_lock(su_home_t *home, const char *file, unsigned int line, const char *function);
71 int _su_home_mutex_unlock(su_home_t *home, const char *file, unsigned int line, const char *function);
72 
73 #define su_home_mutex_lock(home) \
74  _su_home_mutex_lock((home), __FILE__, __LINE__, __func__)
75 
76 #define su_home_mutex_unlock(home) \
77  _su_home_mutex_unlock((home), __FILE__, __LINE__, __func__)
78 
79 
80 su_home_t *_su_home_ref_by(
81  su_home_t *home, char const *file, unsigned line, char const *by);
82 int _su_home_unref_by(
83  su_home_t *home, char const *file, unsigned line, char const *by);
84 
85 #define su_home_ref(home) \
86  _su_home_ref_by((home), __FILE__, __LINE__, __func__)
87 #define su_home_unref(home) \
88  _su_home_unref_by((home), __FILE__, __LINE__, __func__)
89 
90 #else
91 SU_DLL void *su_home_ref(su_home_t const *);
92 SU_DLL int su_home_unref(su_home_t *);
93 #endif
94 
95 SU_DLL size_t su_home_refcount(su_home_t *home);
96 
97 SU_DLL int su_home_destructor(su_home_t *, void (*)(void *));
98 
99 SU_DLL int su_home_desctructor(su_home_t *, void (*)(void *));
100 #ifndef su_home_desctructor
101 /* This has typo in before 1.12.4 */
102 #define su_home_desctructor(home, destructor) \
103  su_home_destructor((home), (destructor))
104 #endif
105 
106 SU_DLL void *su_home_clone(su_home_t *parent, isize_t size)
107  __attribute__((__malloc__));
108 
109 SU_DLL int su_home_init(su_home_t *h);
110 
111 SU_DLL void su_home_deinit(su_home_t *h);
112 
113 SU_DLL void su_home_preload(su_home_t *h, isize_t n, isize_t size);
114 
115 SU_DLL su_home_t *su_home_auto(void *area, isize_t size);
116 
117 #define SU_HOME_AUTO_SIZE(n) \
118  (((n) + ((sizeof(su_home_t) + 7) & (size_t)~8) + \
119  ((3 * sizeof (void *) + 4 * sizeof(unsigned) + \
120  7 * (sizeof (long) + sizeof(void *)) + 7) & (size_t)~8)) \
121  / sizeof(su_home_t))
122 
123 SU_DLL int su_home_move(su_home_t *dst, su_home_t *src);
124 
125 SU_DLL int su_home_threadsafe(su_home_t *home);
126 
127 SU_DLL int su_home_has_parent(su_home_t const *home);
128 
129 SU_DLL su_home_t *su_home_parent(su_home_t const *home);
130 
131 SU_DLL void su_home_check(su_home_t const *home);
132 
133 SU_DLL int su_home_check_alloc(su_home_t const *home, void const *data);
134 
135 #if (!defined(HAVE_MEMLEAK_LOG) || (HAVE_MEMLEAK_LOG != 1))
136 SU_DLL int su_home_mutex_lock(su_home_t *home);
137 
138 SU_DLL int su_home_mutex_unlock(su_home_t *home);
139 #endif
140 
141 SU_DLL int su_home_lock(su_home_t *home);
142 SU_DLL int su_home_trylock(su_home_t *home);
143 SU_DLL int su_home_unlock(su_home_t *home);
144 
145 SU_DLL void *su_alloc(su_home_t *h, isize_t size)
146  __attribute__((__malloc__));
147 SU_DLL void *su_zalloc(su_home_t *h, isize_t size)
148  __attribute__((__malloc__));
149 SU_DLL void *su_salloc(su_home_t *h, isize_t size)
150  __attribute__((__malloc__));
151 SU_DLL void *su_realloc(su_home_t *h, void *data, isize_t size)
152  __attribute__((__malloc__));
153 SU_DLL int su_in_home(su_home_t *h, void const *data);
154 
155 SU_DLL char *su_strdup(su_home_t *home, char const *s)
156  __attribute__((__malloc__));
157 SU_DLL char *su_strcat(su_home_t *home, char const *s1, char const *s2)
158  __attribute__((__malloc__));
159 SU_DLL char *su_strndup(su_home_t *home, char const *s, isize_t n)
160  __attribute__((__malloc__));
161 
162 SU_DLL char *su_strcat_all(su_home_t *home, ...)
163  __attribute__((__malloc__));
164 
165 SU_DLL char *su_sprintf(su_home_t *home, char const *fmt, ...)
166  __attribute__ ((__malloc__, __format__ (printf, 2, 3)));
167 
168 SU_DLL char *su_vsprintf(su_home_t *home, char const *fmt, va_list ap)
169  __attribute__((__malloc__));
170 
171 /* free an independent block */
172 SU_DLL void su_free(su_home_t *h, void *);
173 
175 SU_DLL int su_home_is_threadsafe(su_home_t const *home);
176 
177 /* ---------------------------------------------------------------------- */
178 /* Deprecated */
179 
180 SU_DLL su_home_t *su_home_create(void)
181  __attribute__((__malloc__));
182 SU_DLL void su_home_destroy(su_home_t *h);
183 
184 #define su_home_zap(h) su_home_unref((h))
185 
186 SOFIA_END_DECLS
187 
188 #endif /* ! defined(SU_ALLOC_H) */
char * su_strdup(su_home_t *home, char const *s))
Duplicate a string, allocate memory from home.
Definition: su_strdup.c:52
int su_home_threadsafe(su_home_t *home)
Convert su_home_t object to a thread-safe one.
Definition: su_alloc_lock.c:92
Memory home structure.
Definition: su_alloc.h:57
su_home_t * su_home_create(void))
Create an su_home_t object.
Definition: su_alloc.c:1032
void su_home_destroy(su_home_t *h)
Destroy a home object.
Definition: su_alloc.c:1048
char * su_strcat(su_home_t *home, char const *s1, char const *s2))
Concate two strings, allocate memory for result from home.
Definition: su_strdup.c:78
int su_home_mutex_unlock(su_home_t *home)
Release exclusive lock on home and decrease refcount (if home is threadsafe).
Definition: su_alloc.c:1693
int su_home_has_parent(su_home_t const *home)
Return true if home is a clone.
Definition: su_alloc.c:853
void * su_home_clone(su_home_t *parent, isize_t size))
Clone a su_home_t object.
Definition: su_alloc.c:829
su_home_t * su_home_auto(void *area, isize_t size)
Preload a memory home from stack.
Definition: su_alloc.c:1338
void * su_salloc(su_home_t *h, isize_t size))
Allocate a structure.
Definition: su_alloc.c:1616
int su_home_check_alloc(su_home_t const *home, void const *data)
Check if pointer has been allocated through home.
Definition: su_alloc.c:967
char * su_vsprintf(su_home_t *home, char const *fmt, va_list ap))
Copy a formatted string.
Definition: su_sprintf.c:68
int su_home_move(su_home_t *dst, su_home_t *src)
Move allocations from a su_home_t object to another.
Definition: su_alloc.c:1191
int su_home_trylock(su_home_t *home)
Try to obtain exclusive lock on home without increasing refcount.
Definition: su_alloc.c:1750
void * su_alloc(su_home_t *h, isize_t size))
Allocate a memory block.
Definition: su_alloc.c:885
void su_home_check(su_home_t const *home)
Check home consistency.
Definition: su_alloc.c:991
su_home_t * su_home_parent(su_home_t const *home)
Return home's parent home.
Definition: su_alloc.c:859
int su_in_home(su_home_t *h, void const *data)
Check if a memory block has been allocated from the home.
Definition: su_alloc.c:1531
char * su_sprintf(su_home_t *home, char const *fmt,...)))
Copy a formatted string.
Definition: su_sprintf.c:119
void * su_home_new(isize_t size))
Create a new su_home_t object.
Definition: su_alloc.c:558
size_t su_home_refcount(su_home_t *home)
Return reference count of home.
Definition: su_alloc.c:794
char * su_strndup(su_home_t *home, char const *s, isize_t n))
Duplicate a string with given size, allocate memory from home.
Definition: su_strdup.c:160
void * su_home_ref(su_home_t const *)
Create a new reference to a home object.
Definition: su_alloc.c:723
void * su_realloc(su_home_t *h, void *data, isize_t size))
Reallocate a memory block.
Definition: su_alloc.c:1394
int su_home_unlock(su_home_t *home)
Release exclusive lock on home.
Definition: su_alloc.c:1772
void su_home_deinit(su_home_t *h)
Free memory blocks allocated through home.
Definition: su_alloc.c:1164
int su_home_destructor(su_home_t *, void(*)(void *))
Set destructor function.
Definition: su_alloc.c:591
struct su_alock su_alock_t
Thread-locking function.
Definition: su_alloc.h:54
int su_home_unref(su_home_t *)
Unreference a su_home_t object.
Definition: su_alloc.c:755
int su_home_mutex_lock(su_home_t *home)
Increase refcount and obtain exclusive lock on home.
Definition: su_alloc.c:1659
int su_home_lock(su_home_t *home)
Obtain exclusive lock on home without increasing refcount.
Definition: su_alloc.c:1729
int su_home_desctructor(su_home_t *, void(*)(void *))
Set destructor function.
Definition: su_alloc.c:623
void su_free(su_home_t *h, void *)
Free a memory block.
Definition: su_alloc.c:908
int su_home_is_threadsafe(su_home_t const *home)
Check if a memory home is threadsafe.
Definition: su_alloc.c:1640
SU_HOME_T su_home_t
Memory home type.
Definition: su_alloc.h:50
void * su_zalloc(su_home_t *h, isize_t size))
Allocate and zero a memory block.
Definition: su_alloc.c:1570
void su_home_preload(su_home_t *h, isize_t n, isize_t size)
Preload a memory home.
Definition: su_alloc.c:1306
char * su_strcat_all(su_home_t *home,...))
Concate multiple strings, allocate memory for result from home.
Definition: su_strdup.c:112
int su_home_init(su_home_t *h)
Initialize an su_home_t struct.
Definition: su_alloc.c:1079
Basic integer types for su library.

Sofia-SIP 1.13.17 - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.