icedb  version 0.5.1
Snow particle scattering database API
Namespaces | Functions
util.cpp File Reference
#include "../icedb/defs.h"
#include "../icedb/util.h"
#include "../icedb/util.hpp"
#include <cstring>
#include <cwchar>
#include <signal.h>
Include dependency graph for util.cpp:

Go to the source code of this file.

Namespaces

 icedb
 

Functions

ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s (char *dest, size_t destSz, const char *src, size_t srcSz)
 
ICEDB_SYMBOL_SHARED char * ICEDB_COMPAT_strdup_s (const char *src, size_t srcSz)
 
ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_wcsncpy_s (wchar_t *dest, size_t destSz, const wchar_t *src, size_t srcSz)
 
ICEDB_SYMBOL_SHARED wchar_t * ICEDB_COMPAT_wcsdup_s (const wchar_t *src, size_t srcSz)
 
ICEDB_SYMBOL_SHARED void * ICEDB_malloc (size_t numBytes)
 Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted. More...
 
ICEDB_SYMBOL_SHARED void ICEDB_free (void *obj)
 Free memory region. Should not be double-freed. More...
 
ICEDB_SYMBOL_SHARED void ICEDB_DEBUG_RAISE_EXCEPTION_HANDLER_WC (const wchar_t *file, int line, const wchar_t *fsig)
 
ICEDB_SYMBOL_SHARED void ICEDB_DEBUG_RAISE_EXCEPTION_HANDLER_A (const char *file, int line, const char *fsig)
 
ICEDB_SYMBOL_SHARED void * icedb::_malloc (size_t numBytes)
 
ICEDB_SYMBOL_SHARED void icedb::_free (void *obj)
 

Function Documentation

◆ ICEDB_COMPAT_strdup_s()

ICEDB_SYMBOL_SHARED char* ICEDB_COMPAT_strdup_s ( const char *  src,
size_t  srcSz 
)

Safe char array initialization and copy. Null appended at end (added to srcSz).

Definition at line 42 of file util.cpp.

References ICEDB_COMPAT_strncpy_s(), ICEDB_DEBUG_RAISE_EXCEPTION, and ICEDB_malloc().

Referenced by error_context_add_string(), and ICEDB_enumModules().

43 {
45  if (!src) ICEDB_DEBUG_RAISE_EXCEPTION();
46  char* res = (char*)ICEDB_malloc(sizeof(char)*(srcSz + 1));
47  ICEDB_COMPAT_strncpy_s(res, srcSz + 1, src, srcSz + 1);
48  return res;
49 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
ICEDB_SYMBOL_SHARED void * ICEDB_malloc(size_t numBytes)
Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted...
Definition: util.cpp:90
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_COMPAT_strncpy_s()

ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s ( char *  dest,
size_t  destSz,
const char *  src,
size_t  srcSz 
)

Safe char array copy.

Returns
the number of characters actually written.
Parameters
destis the pointer to the destination. Always null terminated.
destSzis the size of the destination buller, including the trailing null character.
srcis the pointer to the source. Characters from src are copied either until the first null character or until srcSz. Note that null termination comes later.
srcSzis the max size of the source buffer.

Definition at line 14 of file util.cpp.

References ICEDB_DEBUG_RAISE_EXCEPTION.

Referenced by error_code_to_message(), error_context_append(), error_context_copy(), error_context_create_impl(), error_context_to_message(), error_context_to_message_size(), error_context_widen(), icedb::versioning::genVersionInfo(), ICEDB_COMPAT_strdup_s(), ICEDB_findModuleByFunc(), ICEDB_getAppDir(), ICEDB_getAppPath(), ICEDB_getCWD(), ICEDB_getLibDir(), ICEDB_getLibPath(), ICEDB_getPluginDir(), and ICEDB_WriteLibVersionInfoC().

17 {
26  if (!dest || !src)ICEDB_DEBUG_RAISE_EXCEPTION();
27 #if ICEDB_USING_SECURE_STRINGS
28  strncpy_s(dest, destSz, src, srcSz);
29 #else
30  if (srcSz <= destSz) {
31  strncpy(dest, src, srcSz);
32  }
33  else {
34  strncpy(dest, src, destSz);
35  }
36 #endif
37  dest[destSz - 1] = 0;
38  for (size_t i = 0; i < destSz; ++i) if (dest[i] == '\0') return i;
39  return 0; // Should never be reached
40 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
Here is the caller graph for this function:

◆ ICEDB_COMPAT_wcsdup_s()

ICEDB_SYMBOL_SHARED wchar_t* ICEDB_COMPAT_wcsdup_s ( const wchar_t *  src,
size_t  srcSz 
)

Safe char array initialization and copy. Null appended at end (added to srcSz).

Definition at line 80 of file util.cpp.

References ICEDB_COMPAT_wcsncpy_s(), ICEDB_DEBUG_RAISE_EXCEPTION, and ICEDB_malloc().

81 {
83  if (!src) ICEDB_DEBUG_RAISE_EXCEPTION();
84  wchar_t* res = (wchar_t*)ICEDB_malloc(sizeof(wchar_t)*(srcSz + 1));
85  ICEDB_COMPAT_wcsncpy_s(res, srcSz + 1, src, srcSz + 1);
86  return res;
87 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
ICEDB_SYMBOL_SHARED void * ICEDB_malloc(size_t numBytes)
Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted...
Definition: util.cpp:90
ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_wcsncpy_s(wchar_t *dest, size_t destSz, const wchar_t *src, size_t srcSz)
Definition: util.cpp:52
Here is the call graph for this function:

◆ ICEDB_COMPAT_wcsncpy_s()

ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_wcsncpy_s ( wchar_t *  dest,
size_t  destSz,
const wchar_t *  src,
size_t  srcSz 
)

Safe char array copy.

Returns
the number of characters actually written.
Parameters
destis the pointer to the destination. Always null terminated.
destSzis the size of the destination buller, including the trailing null character.
srcis the pointer to the source. Characters from src are copied either until the first null character or until srcSz. Note that null termination comes later.
srcSzis the max size of the source buffer.

Definition at line 52 of file util.cpp.

References ICEDB_DEBUG_RAISE_EXCEPTION.

Referenced by ICEDB_COMPAT_wcsdup_s().

55 {
64  if (!dest || !src)ICEDB_DEBUG_RAISE_EXCEPTION();
65 #if ICEDB_USING_SECURE_STRINGS
66  wcsncpy_s(dest, destSz, src, srcSz);
67 #else
68  if (srcSz <= destSz) {
69  wcsncpy(dest, src, srcSz);
70  }
71  else {
72  wcsncpy(dest, src, destSz);
73  }
74 #endif
75  dest[destSz - 1] = 0;
76  for (size_t i = 0; i < destSz; ++i) if (dest[i] == '\0') return i;
77  return 0; // Should never be reached
78 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
Here is the caller graph for this function:

◆ ICEDB_DEBUG_RAISE_EXCEPTION_HANDLER_A()

ICEDB_SYMBOL_SHARED void ICEDB_DEBUG_RAISE_EXCEPTION_HANDLER_A ( const char *  file,
int  line,
const char *  fsig 
)

Definition at line 113 of file util.cpp.

References ICEDB_BEGIN_DECL, ICEDB_COMPAT_fprintf_s, and ICEDB_END_DECL_C.

114 {
115  ICEDB_COMPAT_fprintf_s(stderr, "Exception raised in file %s, line %d, function %s.\n", file, line, fsig);
116 
117 #ifdef _WIN32
118  DebugBreak();
119 #else
120  raise(SIGTRAP);
121  exit(999);
122 #endif
123 }
#define ICEDB_COMPAT_fprintf_s
Definition: util.h:26

◆ ICEDB_DEBUG_RAISE_EXCEPTION_HANDLER_WC()

ICEDB_SYMBOL_SHARED void ICEDB_DEBUG_RAISE_EXCEPTION_HANDLER_WC ( const wchar_t *  file,
int  line,
const wchar_t *  fsig 
)

Definition at line 102 of file util.cpp.

References ICEDB_COMPAT_fwprintf_s.

103 {
104  ICEDB_COMPAT_fwprintf_s(stderr, L"Exception raised in file %s, line %d, function %s.\n", file, line, fsig);
105 
106 #ifdef _WIN32
107  DebugBreak();
108 #else
109  raise(SIGTRAP);
110  exit(999);
111 #endif
112 }
#define ICEDB_COMPAT_fwprintf_s
Definition: util.h:27

◆ ICEDB_free()

ICEDB_SYMBOL_SHARED void ICEDB_free ( void *  obj)

Free memory region. Should not be double-freed.

Definition at line 97 of file util.cpp.

References icedb::free(), and ICEDB_DEBUG_RAISE_EXCEPTION.

Referenced by icedb::_free(), error_context_append(), error_context_create_impl(), error_context_deallocate(), error_context_to_message(), error_context_to_message_size(), error_context_widen(), ICEDB_free_enumModulesRes(), and icedb::os_functions::populateOSstrings().

98 {
99  if (!obj) ICEDB_DEBUG_RAISE_EXCEPTION();
100  free(obj);
101 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
void free(T *obj)
Definition: util.hpp:21
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ICEDB_malloc()

ICEDB_SYMBOL_SHARED void* ICEDB_malloc ( size_t  numBytes)

Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted.

Definition at line 90 of file util.cpp.

References ICEDB_DEBUG_RAISE_EXCEPTION, and icedb::malloc().

Referenced by icedb::_malloc(), error_context_append(), error_context_copy(), error_context_create_impl(), error_context_to_message(), error_context_to_message_size(), error_context_widen(), ICEDB_COMPAT_strdup_s(), ICEDB_COMPAT_wcsdup_s(), ICEDB_enumModules(), and icedb::os_functions::populateOSstrings().

91 {
92  void* res = malloc(numBytes);
93  if (!res) ICEDB_DEBUG_RAISE_EXCEPTION();
94  return res;
95 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
T * malloc(size_t numBytes)
Definition: util.hpp:13
Here is the call graph for this function:
Here is the caller graph for this function: