ckd_alloc.h File Reference

Sphinx's memory allocation/deallocation routines. More...

`#include <stdlib.h>`
`#include <setjmp.h>`
`#include <sphinxbase/sphinxbase_export.h>`
`#include <sphinxbase/prim_type.h>`

Go to the source code of this file.

## Macros

#define | ckd_calloc(n, sz)
| Macros to simplify the use of above functions.
#define | ckd_malloc(sz)
| Macro for ckd_malloc.
#define | ckd_realloc(ptr, sz)
| Macro for ckd_realloc.
#define | ckd_salloc(ptr)
| Macro for ckd_salloc.
#define | ckd_calloc_2d(d1, d2, sz)
| Macro for ckd_calloc_2d.
#define | ckd_calloc_3d(d1, d2, d3, sz)
| Macro for ckd_calloc_3d.
#define | ckd_calloc_4d(d1, d2, d3, d4, s)
| Macro for ckd_calloc_4d.
#define | ckd_alloc_2d_ptr(d1, d2, bf, sz)
| Macro for ckd_alloc_2d_ptr.
#define | ckd_free_2d_ptr(bf)
| Free only the pointer arrays allocated with ckd_alloc_2d_ptr().
#define | ckd_alloc_3d_ptr(d1, d2, d3, bf, sz)
| Macro for ckd_alloc_3d_ptr.
#define | ckd_free_3d_ptr(bf)
| Free only the pointer arrays allocated with ckd_alloc_3d_ptr().

## Functions

jmp_buf * | ckd_set_jump (jmp_buf *env, int abort)
| Control behaviour of the program when allocation fails.
void | ckd_fail (char *format,...)
| Fail (with a message) according to behaviour specified by ckd_set_jump().
SPHINXBASE_EXPORT void * | __ckd_calloc__ (size_t n_elem, size_t elem_size, const char *caller_file, int caller_line)
SPHINXBASE_EXPORT void * | __ckd_malloc__ (size_t size, const char *caller_file, int caller_line)
SPHINXBASE_EXPORT void * | __ckd_realloc__ (void *ptr, size_t new_size, const char *caller_file, int caller_line)
SPHINXBASE_EXPORT char * | __ckd_salloc__ (const char *origstr, const char *caller_file, int caller_line)
| Like strdup, except that if an error occurs it prints a diagnostic message and exits.
SPHINXBASE_EXPORT void * | __ckd_calloc_2d__ (size_t d1, size_t d2, size_t elemsize, const char *caller_file, int caller_line)
| Allocate a 2-D array and return ptr to it (ie, ptr to vector of ptrs).
SPHINXBASE_EXPORT void * | __ckd_calloc_3d__ (size_t d1, size_t d2, size_t d3, size_t elemsize, const char *caller_file, int caller_line)
| Allocate a 3-D array and return ptr to it.
SPHINXBASE_EXPORT void **** | __ckd_calloc_4d__ (size_t d1, size_t d2, size_t d3, size_t d4, size_t elem_size, char *caller_file, int caller_line)
| Allocate a 34D array and return ptr to it.
SPHINXBASE_EXPORT void * | __ckd_alloc_3d_ptr (size_t d1, size_t d2, size_t d3, void *store, size_t elem_size, char *caller_file, int caller_line)
| Overlay a 3-D array over a previously allocated storage area.
SPHINXBASE_EXPORT void * | __ckd_alloc_2d_ptr (size_t d1, size_t d2, void *store, size_t elem_size, char *caller_file, int caller_line)
| Overlay a s-D array over a previously allocated storage area.
SPHINXBASE_EXPORT void | ckd_free (void *ptr)
| Test and free a 1-D array.
SPHINXBASE_EXPORT void | ckd_free_2d (void *ptr)
| Free a 2-D array (ptr) previously allocated by ckd_calloc_2d.
SPHINXBASE_EXPORT void | ckd_free_3d (void *ptr)
| Free a 3-D array (ptr) previously allocated by ckd_calloc_3d.
SPHINXBASE_EXPORT void | ckd_free_4d (void *ptr)
| Free a 4-D array (ptr) previously allocated by ckd_calloc_4d.

## Detailed Description

Sphinx's memory allocation/deallocation routines.

Implementation of efficient memory allocation deallocation for multiple dimensional arrays.

Definition in file ckd_alloc.h.

## Macro Definition Documentation

## ckd_alloc_2d_ptr

#define ckd_alloc_2d_ptr | ( |  | d1,
|  |  | d2,
|  |  | bf,
|  |  | sz )

Value:
__ckd_alloc_2d_ptr((d1), (d2), (bf), (sz), __FILE__, __LINE__)
__ckd_alloc_2d_ptr
SPHINXBASE_EXPORT void * __ckd_alloc_2d_ptr(size_t d1, size_t d2, void *store, size_t elem_size, char *caller_file, int caller_line)
Overlay a s-D array over a previously allocated storage area.
Definition ckd_alloc.c:408

Macro for ckd_alloc_2d_ptr.

Definition at line 287 of file ckd_alloc.h.

Referenced by bio_fread_2d().

## ckd_alloc_3d_ptr

#define ckd_alloc_3d_ptr | ( |  | d1,
|  |  | d2,
|  |  | d3,
|  |  | bf,
|  |  | sz )

Value:
__ckd_alloc_3d_ptr((d1), (d2), (d3), (bf), (sz), __FILE__, __LINE__)
__ckd_alloc_3d_ptr
SPHINXBASE_EXPORT void * __ckd_alloc_3d_ptr(size_t d1, size_t d2, size_t d3, void *store, size_t elem_size, char *caller_file, int caller_line)
Overlay a 3-D array over a previously allocated storage area.
Definition ckd_alloc.c:380

Macro for ckd_alloc_3d_ptr.

Definition at line 298 of file ckd_alloc.h.

Referenced by bio_fread_3d().

## ckd_calloc

#define ckd_calloc | ( |  | n,
|  |  | sz )

Value:
__ckd_calloc__((n),(sz),__FILE__,__LINE__)

Macros to simplify the use of above functions.

One should use these, rather than target functions directly. Macro for ckd_calloc

Definition at line 248 of file ckd_alloc.h.

Referenced by __ckd_calloc_4d__(), agc_init(), bio_fread_1d(), bio_fwrite(), bio_read_wavfile(), bio_readhdr(), bit_encode_attach(), cmd_ln_init(), cmd_ln_parse_file_r(), cmd_ln_parse_r(), feat_array_alloc(), feat_init(), feat_lda_transform(), feat_s2mfc2feat(), feat_set_subvecs(), fopen_compchk(), glist_add_float32(), glist_add_float64(), glist_add_int32(), glist_add_ptr(), glist_add_uint32(), glist_insert_float32(), glist_insert_float64(), glist_insert_int32(), glist_insert_ptr(), glist_insert_uint32(), hash_table_iter(), hash_table_new(), heap_new(), jsgf_grammar_new(), lineiter_start(), listelem_alloc_init(), logmath_init(), logmath_read(), mmio_file_read(), ngram_model_add_class(), ngram_model_set_init(), ngram_model_set_iter(), ngram_model_set_map_words(), ngram_model_set_read(), ngram_prob(), ngram_probv(), ngram_score(), parse_subvecs(), pctr_new(), sbevent_init(), sbmsgq_init(), sbmtx_init(), sbthread_start(), string_join(), and yin_init().

## ckd_calloc_2d

#define ckd_calloc_2d | ( |  | d1,
|  |  | d2,
|  |  | sz )

Value:
__ckd_calloc_2d__((d1),(d2),(sz),__FILE__,__LINE__)
__ckd_calloc_2d__
SPHINXBASE_EXPORT void * __ckd_calloc_2d__(size_t d1, size_t d2, size_t elemsize, const char *caller_file, int caller_line)
Allocate a 2-D array and return ptr to it (ie, ptr to vector of ptrs).
Definition ckd_alloc.c:223

Macro for ckd_calloc_2d.

Definition at line 270 of file ckd_alloc.h.

Referenced by feat_array_alloc(), feat_init(), ngram_model_set_add(), ngram_model_set_map_words(), and yin_init().

## ckd_calloc_3d

#define ckd_calloc_3d | ( |  | d1,
|  |  | d2,
|  |  | d3,
|  |  | sz )

Value:
__ckd_calloc_3d__((d1),(d2),(d3),(sz),__FILE__,__LINE__)
__ckd_calloc_3d__
SPHINXBASE_EXPORT void * __ckd_calloc_3d__(size_t d1, size_t d2, size_t d3, size_t elemsize, const char *caller_file, int caller_line)
Allocate a 3-D array and return ptr to it.
Definition ckd_alloc.c:265

Macro for ckd_calloc_3d.

Definition at line 276 of file ckd_alloc.h.

## ckd_calloc_4d

#define ckd_calloc_4d | ( |  | d1,
|  |  | d2,
|  |  | d3,
|  |  | d4,
|  |  | s )

Value:
__ckd_calloc_4d__((d1), (d2), (d3), (d4), (s), __FILE__, __LINE__)
__ckd_calloc_4d__
SPHINXBASE_EXPORT void **** __ckd_calloc_4d__(size_t d1, size_t d2, size_t d3, size_t d4, size_t elem_size, char *caller_file, int caller_line)
Allocate a 34D array and return ptr to it.
Definition ckd_alloc.c:310

Macro for ckd_calloc_4d.

Definition at line 281 of file ckd_alloc.h.

## ckd_free_2d_ptr

#define ckd_free_2d_ptr | ( |  | bf | ) |

Value:
ckd_free(bf)
ckd_free
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
Definition ckd_alloc.c:244

Free only the pointer arrays allocated with ckd_alloc_2d_ptr().

Definition at line 292 of file ckd_alloc.h.

## ckd_free_3d_ptr

#define ckd_free_3d_ptr | ( |  | bf | ) |

Value:
ckd_free_2d(bf)
ckd_free_2d
SPHINXBASE_EXPORT void ckd_free_2d(void *ptr)
Free a 2-D array (ptr) previously allocated by ckd_calloc_2d.
Definition ckd_alloc.c:255

Free only the pointer arrays allocated with ckd_alloc_3d_ptr().

Definition at line 303 of file ckd_alloc.h.

## ckd_malloc

#define ckd_malloc | ( |  | sz | ) |

Value:
__ckd_malloc__((sz),__FILE__,__LINE__)

Macro for ckd_malloc.

Definition at line 253 of file ckd_alloc.h.

Referenced by fread_line(), and lineiter_start().

## ckd_realloc

#define ckd_realloc | ( |  | ptr,
|  |  | sz )

Value:
__ckd_realloc__(ptr,(sz),__FILE__,__LINE__)

Macro for ckd_realloc.

Definition at line 258 of file ckd_alloc.h.

Referenced by bitvec_realloc(), cmd_ln_parse_file_r(), feat_s2mfc2feat(), fread_line(), ngram_model_add_class(), and ngram_model_set_add().

## ckd_salloc

#define ckd_salloc | ( |  | ptr | ) |

Value:
__ckd_salloc__(ptr,__FILE__,__LINE__)
__ckd_salloc__
SPHINXBASE_EXPORT char * __ckd_salloc__(const char *origstr, const char *caller_file, int caller_line)
Like strdup, except that if an error occurs it prints a diagnostic message and exits.
Definition ckd_alloc.c:205

Macro for ckd_salloc.

Definition at line 264 of file ckd_alloc.h.

Referenced by bio_readhdr(), build_directory(), cmd_ln_init(), cmd_ln_parse_file_r(), cmd_ln_set_str_extra_r(), cmd_ln_set_str_r(), feat_init(), ngram_model_casefold(), ngram_model_set_add(), ngram_model_set_init(), ngram_model_set_map_words(), ngram_model_set_read(), and pctr_new().

## Function Documentation

## __ckd_alloc_2d_ptr()

SPHINXBASE_EXPORT void * __ckd_alloc_2d_ptr | ( | size_t | d1,
|  | size_t | d2,
|  | void * | store,
|  | size_t | elem_size,
|  | char * | caller_file,
|  | int | caller_line )

Overlay a s-D array over a previously allocated storage area.

Definition at line 408 of file ckd_alloc.c.

References __ckd_alloc_2d_ptr().

Referenced by __ckd_alloc_2d_ptr().

## __ckd_alloc_3d_ptr()

SPHINXBASE_EXPORT void * __ckd_alloc_3d_ptr | ( | size_t | d1,
|  | size_t | d2,
|  | size_t | d3,
|  | void * | store,
|  | size_t | elem_size,
|  | char * | caller_file,
|  | int | caller_line )

Overlay a 3-D array over a previously allocated storage area.

Definition at line 380 of file ckd_alloc.c.

References __ckd_alloc_3d_ptr().

Referenced by __ckd_alloc_3d_ptr().

## __ckd_calloc_2d__()

SPHINXBASE_EXPORT void * __ckd_calloc_2d__ | ( | size_t | d1,
|  | size_t | d2,
|  | size_t | elemsize,
|  | const char * | caller_file,
|  | int | caller_line )

Allocate a 2-D array and return ptr to it (ie, ptr to vector of ptrs).

The data area is allocated in one block so it can also be treated as a 1-D array.

Definition at line 223 of file ckd_alloc.c.

References __ckd_calloc_2d__().

Referenced by __ckd_calloc_2d__().

## __ckd_calloc_3d__()

SPHINXBASE_EXPORT void * __ckd_calloc_3d__ | ( | size_t | d1,
|  | size_t | d2,
|  | size_t | d3,
|  | size_t | elemsize,
|  | const char * | caller_file,
|  | int | caller_line )

Allocate a 3-D array and return ptr to it.

The data area is allocated in one block so it can also be treated as a 1-D array.

Definition at line 265 of file ckd_alloc.c.

References __ckd_calloc_3d__().

Referenced by __ckd_calloc_3d__().

## __ckd_calloc_4d__()

SPHINXBASE_EXPORT void **** __ckd_calloc_4d__ | ( | size_t | d1,
|  | size_t | d2,
|  | size_t | d3,
|  | size_t | d4,
|  | size_t | elem_size,
|  | char * | caller_file,
|  | int | caller_line )

Allocate a 34D array and return ptr to it.

The data area is allocated in one block so it can also be treated as a 1-D array.

Definition at line 310 of file ckd_alloc.c.

References __ckd_calloc_4d__(), ckd_calloc, and E_FATAL.

Referenced by __ckd_calloc_4d__().

## __ckd_calloc__()

SPHINXBASE_EXPORT void * __ckd_calloc__ | ( | size_t | n_elem,
|  | size_t | elem_size,
|  | const char * | caller_file,
|  | int | caller_line )

Definition at line 144 of file ckd_alloc.c.

## __ckd_malloc__()

SPHINXBASE_EXPORT void * __ckd_malloc__ | ( | size_t | size,
|  | const char * | caller_file,
|  | int | caller_line )

Definition at line 169 of file ckd_alloc.c.

## __ckd_realloc__()

SPHINXBASE_EXPORT void * __ckd_realloc__ | ( | void * | ptr,
|  | size_t | new_size,
|  | const char * | caller_file,
|  | int | caller_line )

Definition at line 187 of file ckd_alloc.c.

## __ckd_salloc__()

SPHINXBASE_EXPORT char * __ckd_salloc__ | ( | const char * | origstr,
|  | const char * | caller_file,
|  | int | caller_line )

Like strdup, except that if an error occurs it prints a diagnostic message and exits.

If origin in NULL the function also returns NULL.

Definition at line 205 of file ckd_alloc.c.

References __ckd_salloc__().

Referenced by __ckd_salloc__().

## ckd_fail()

void ckd_fail | ( | char * | format,
|  |  | ... )

Fail (with a message) according to behaviour specified by ckd_set_jump().

Definition at line 122 of file ckd_alloc.c.

References ckd_fail().

Referenced by ckd_fail().

## ckd_free()

SPHINXBASE_EXPORT void ckd_free | ( | void * | ptr | ) |

Test and free a 1-D array.

Definition at line 244 of file ckd_alloc.c.

References ckd_free().

Referenced by agc_free(), bio_fwrite(), bio_hdrarg_free(), bio_read_wavfile(), bit_encode_free(), build_directory(), ckd_free(), ckd_free_2d(), ckd_free_3d(), ckd_free_4d(), cmd_ln_free_r(), cmd_ln_parse_file_r(), cmd_ln_set_str_extra_r(), cmd_ln_set_str_r(), feat_array_free(), feat_free(), feat_init(), feat_lda_transform(), feat_s2mfc2feat(), feat_set_subvecs(), fopen_comp(), fopen_compchk(), glist_free(), gnode_free(), hash_table_delete_bkey(), hash_table_empty(), hash_table_enter_bkey(), hash_table_free(), hash_table_iter_free(), hash_table_lookup_bkey(), hash_table_replace_bkey(), heap_destroy(), jsgf_get_rule(), jsgf_grammar_free(), lineiter_free(), listelem_alloc_free(), listelem_alloc_init(), logmath_free(), mmio_file_unmap(), ngram_model_free(), ngram_model_read_classdef(), ngram_model_set_iter_free(), ngram_model_set_map_words(), ngram_model_set_read(), ngram_model_set_remove(), ngram_prob(), ngram_probv(), ngram_score(), pctr_free(), sbevent_free(), sbevent_init(), sbmsgq_free(), sbmsgq_init(), sbmtx_free(), sbmtx_init(), sbthread_free(), subvecs_free(), and yin_free().

## ckd_free_2d()

SPHINXBASE_EXPORT void ckd_free_2d | ( | void * | ptr | ) |

Free a 2-D array (ptr) previously allocated by ckd_calloc_2d.

Definition at line 255 of file ckd_alloc.c.

References ckd_free(), and ckd_free_2d().

Referenced by ckd_free_2d(), feat_array_free(), feat_free(), feat_s2mfc2feat(), ngram_model_set_add(), ngram_model_set_map_words(), and yin_free().

## ckd_free_3d()

SPHINXBASE_EXPORT void ckd_free_3d | ( | void * | ptr | ) |

Free a 3-D array (ptr) previously allocated by ckd_calloc_3d.

Definition at line 297 of file ckd_alloc.c.

References ckd_free(), and ckd_free_3d().

Referenced by ckd_free_3d(), feat_free(), and feat_read_lda().

## ckd_free_4d()

SPHINXBASE_EXPORT void ckd_free_4d | ( | void * | ptr | ) |

Free a 4-D array (ptr) previously allocated by ckd_calloc_4d.

Definition at line 364 of file ckd_alloc.c.

References ckd_free(), and ckd_free_4d().

Referenced by ckd_free_4d().

## ckd_set_jump()

jmp_buf * ckd_set_jump | ( | jmp_buf * | env,
|  | int | abort )

Control behaviour of the program when allocation fails.

Although your program is probably toast when memory allocation fails, it is also probably a good idea to be able to catch these errors and alert the user in some way. Either that, or you might want the program to call abort() so that you can debug the failed code. This function allows you to control that behaviour.

Parameters

  env | Pointer to a `jmp_buf` initialized with setjmp(), or NULL to remove a previously set jump target.
  abort | If non-zero, the program will call abort() when allocation fails rather than exiting or calling longjmp().

Returns
  Pointer to a previously set `jmp_buf`, if any.

Definition at line 109 of file ckd_alloc.c.

References ckd_set_jump().

Referenced by ckd_set_jump().