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 #if defined(__linux__) && !defined(__USE_BSD) 43 #define __USE_BSD 44 #endif 45 46 #include "ftdm_declare.h" 47 #include "ftdm_threadmutex.h" 48 #include <string.h> 49 50 #ifndef __WINDOWS__ 51 #include <unistd.h> 52 #endif 53 54 /*! \brief time data type */ 55 typedef uint64_t ftdm_time_t; 56 57 /*! \brief sleep x amount of milliseconds */ 58 #ifdef __WINDOWS__ 59 #define ftdm_sleep(x) Sleep(x) 60 #else 61 #define ftdm_sleep(x) usleep(x * 1000) 62 #endif 63 64 /*! \brief strncpy replacement */ 65 #define ftdm_copy_string(x,y,z) strncpy(x, y, z - 1) 66 67 /*! \brief strncpy into a fixed-length buffer */ 68 #define ftdm_set_string(x,y) strncpy(x, y, sizeof(x)-1) 69 70 /*! \brief check for null or zero length string buffer */ 71 #define ftdm_strlen_zero(s) (!s || *s == '\0') 72 73 /*! \brief check for zero length string buffer */ 74 #define ftdm_strlen_zero_buf(s) (*s == '\0') 75 76 /*! \brief array len helper */ 77 #define ftdm_array_len(array) sizeof(array)/sizeof(array[0]) 78 79 /*! \brief The memory handler. 80 Do not use directly this variable, use the memory macros and ftdm_global_set_memory_handler to override */ 81 FT_DECLARE_DATA extern ftdm_memory_handler_t g_ftdm_mem_handler; 82 83 /*! 84 \brief Allocate uninitialized memory 85 \param chunksize the chunk size 86 */ 87 #define ftdm_malloc(chunksize) g_ftdm_mem_handler.malloc(g_ftdm_mem_handler.pool, chunksize) 88 89 /*! 90 \brief Reallocates memory 91 \param buff the buffer 92 \param chunksize the chunk size 93 */ 94 #define ftdm_realloc(buff, chunksize) g_ftdm_mem_handler.realloc(g_ftdm_mem_handler.pool, buff, chunksize) 95 96 /*! 97 \brief Allocate initialized memory 98 \param chunksize the chunk size 99 */ 100 #define ftdm_calloc(elements, chunksize) g_ftdm_mem_handler.calloc(g_ftdm_mem_handler.pool, elements, chunksize) 101 102 /*! 103 \brief Free chunk of memory 104 \param chunksize the chunk size 105 */ 106 #define ftdm_free(chunk) g_ftdm_mem_handler.free(g_ftdm_mem_handler.pool, chunk) 107 108 /*! 109 \brief Free a pointer and set it to NULL unless it already is NULL 110 \param it the pointer 111 */ 112 #define ftdm_safe_free(it) if (it) { ftdm_free(it); it = NULL; } 113 114 /*! \brief Duplicate string */ 115 FT_DECLARE(char *) ftdm_strdup(const char *str); 116 117 /*! \brief Duplicate string with limit */ 118 FT_DECLARE(char *) ftdm_strndup(const char *str, ftdm_size_t inlen); 119 120 /*! \brief Get the current time in milliseconds */ 121 FT_DECLARE(ftdm_time_t) ftdm_current_time_in_ms(void); 122 123 #ifdef __cplusplus 124 } /* extern C */ 125 #endif 126 127 #endif 128 129 /* For Emacs: 130 * Local Variables: 131 * mode:c 132 * indent-tabs-mode:t 133 * tab-width:4 134 * c-basic-offset:4 135 * End: 136 * For VIM: 137 * vim:set softtabstop=4 shiftwidth=4 tabstop=4: 138 */