… | |
… | |
196 | See the description of C<ev_embed> watchers for more info. |
196 | See the description of C<ev_embed> watchers for more info. |
197 | |
197 | |
198 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
198 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
199 | |
199 | |
200 | Sets the allocation function to use (the prototype is similar - the |
200 | Sets the allocation function to use (the prototype is similar - the |
201 | semantics is identical - to the realloc C function). It is used to |
201 | semantics are identical to the C<realloc> C89/SuS/POSIX function). It is |
202 | allocate and free memory (no surprises here). If it returns zero when |
202 | used to allocate and free memory (no surprises here). If it returns zero |
203 | memory needs to be allocated, the library might abort or take some |
203 | when memory needs to be allocated (C<size != 0>), the library might abort |
204 | potentially destructive action. The default is your system realloc |
204 | or take some potentially destructive action. |
205 | function. |
205 | |
|
|
206 | Since some systems (at least OpenBSD and Darwin) fail to implement |
|
|
207 | correct C<realloc> semantics, libev will use a wrapper around the system |
|
|
208 | C<realloc> and C<free> functions by default. |
206 | |
209 | |
207 | You could override this function in high-availability programs to, say, |
210 | You could override this function in high-availability programs to, say, |
208 | free some memory if it cannot allocate memory, to use a special allocator, |
211 | free some memory if it cannot allocate memory, to use a special allocator, |
209 | or even to sleep a while and retry until some memory is available. |
212 | or even to sleep a while and retry until some memory is available. |
210 | |
213 | |
211 | Example: Replace the libev allocator with one that waits a bit and then |
214 | Example: Replace the libev allocator with one that waits a bit and then |
212 | retries). |
215 | retries (example requires a standards-compliant C<realloc>). |
213 | |
216 | |
214 | static void * |
217 | static void * |
215 | persistent_realloc (void *ptr, size_t size) |
218 | persistent_realloc (void *ptr, size_t size) |
216 | { |
219 | { |
217 | for (;;) |
220 | for (;;) |