1 /* 2 * Copyright (c) 2010, Sangoma Technologies 3 * Moises Silva <moy@sangoma.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * * Neither the name of the original author; nor the names of any contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 26 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #ifndef __FTDM_OS_H__ 36 #define __FTDM_OS_H__ 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 #include "ftdm_declare.h" 43 #include "ftdm_threadmutex.h" 44 45 #include <string.h> 46 47 #ifndef __WINDOWS__ 48 #include <unistd.h> 49 #endif 50 51 /*! \brief sleep x amount of milliseconds */ 52 #ifdef __WINDOWS__ 53 #define ftdm_sleep(x) Sleep(x) 54 #else 55 #define ftdm_sleep(x) usleep(x * 1000) 56 #endif 57 58 /*! \brief strncpy replacement */ 59 #define ftdm_copy_string(x,y,z) strncpy(x, y, z - 1) 60 61 /*! \brief strncpy into a fixed-length buffer */ 62 #define ftdm_set_string(x,y) strncpy(x, y, sizeof(x)-1) 63 64 /*! \brief check for null or zero length string buffer */ 65 #define ftdm_strlen_zero(s) (!s || *s == '\0') 66 67 /*! \brief check for zero length string buffer */ 68 #define ftdm_strlen_zero_buf(s) (*s == '\0') 69 70 /*! \brief array len helper */ 71 #define ftdm_array_len(array) sizeof(array)/sizeof(array[0]) 72 73 /*! \brief The memory handler. 74 Do not use directly this variable, use the memory macros and ftdm_global_set_memory_handler to override */ 75 FT_DECLARE_DATA extern ftdm_memory_handler_t g_ftdm_mem_handler; 76 77 /*! 78 \brief Allocate uninitialized memory 79 \param chunksize the chunk size 80 */ 81 #define ftdm_malloc(chunksize) g_ftdm_mem_handler.malloc(g_ftdm_mem_handler.pool, chunksize) 82 83 /*! 84 \brief Reallocates memory 85 \param buff the buffer 86 \param chunksize the chunk size 87 */ 88 #define ftdm_realloc(buff, chunksize) g_ftdm_mem_handler.realloc(g_ftdm_mem_handler.pool, buff, chunksize) 89 90 /*! 91 \brief Allocate initialized memory 92 \param chunksize the chunk size 93 */ 94 #define ftdm_calloc(elements, chunksize) g_ftdm_mem_handler.calloc(g_ftdm_mem_handler.pool, elements, chunksize) 95 96 /*! 97 \brief Free chunk of memory 98 \param chunksize the chunk size 99 */ 100 #define ftdm_free(chunk) g_ftdm_mem_handler.free(g_ftdm_mem_handler.pool, chunk) 101 102 /*! 103 \brief Free a pointer and set it to NULL unless it already is NULL 104 \param it the pointer 105 */ 106 #define ftdm_safe_free(it) if (it) { ftdm_free(it); it = NULL; } 107 108 /*! \brief Duplicate string */ 109 FT_DECLARE(char *) ftdm_strdup(const char *str); 110 111 /*! \brief Duplicate string with limit */ 112 FT_DECLARE(char *) ftdm_strndup(const char *str, ftdm_size_t inlen); 113 114 115 #ifdef __cplusplus 116 } /* extern C */ 117 #endif 118 119 #endif 120 121 /* For Emacs: 122 * Local Variables: 123 * mode:c 124 * indent-tabs-mode:t 125 * tab-width:4 126 * c-basic-offset:4 127 * End: 128 * For VIM: 129 * vim:set softtabstop=4 shiftwidth=4 tabstop=4: 130 */