Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os by
os_port.c
00001 /* 00002 * Copyright (c) 2007, Cameron Rich 00003 * 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 00009 * * Redistributions of source code must retain the above copyright notice, 00010 * this list of conditions and the following disclaimer. 00011 * * Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * * Neither the name of the axTLS project nor the names of its contributors 00015 * may be used to endorse or promote products derived from this software 00016 * without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00026 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 /** 00032 * @file os_port.c 00033 * 00034 * OS specific functions. 00035 */ 00036 #include <time.h> 00037 #include <stdlib.h> 00038 #include <errno.h> 00039 #include <stdarg.h> 00040 #include "os_port.h" 00041 #include <stdio.h> 00042 #include "sockets.h" 00043 00044 static int memory_buf[400]; 00045 static char enable = 1; 00046 static int nb_entries = 0; 00047 static int nb_alloc = 0; 00048 00049 void disable_memory_buf(void) 00050 { 00051 enable = 0; 00052 } 00053 void enable_memory_buf(void) 00054 { 00055 enable = 1; 00056 } 00057 void init_memory_buf(void) 00058 { 00059 for(int i = 0; i < 400; i += 2) 00060 { 00061 memory_buf[i] = -1; 00062 memory_buf[i+1] = 0; 00063 } 00064 } 00065 void print_buf_stats(void) 00066 { 00067 if(enable) 00068 { 00069 int used = 0; 00070 for(int i = 1; i < 400; i += 2) 00071 used += memory_buf[i]; 00072 printf("%d\n", used); 00073 } 00074 } 00075 00076 void print_all_buf_stats(void) 00077 { 00078 int used = 0; 00079 for(int i = 1; i < 400; i += 2) 00080 used += memory_buf[i]; 00081 printf("used: %d bytes\n", used); 00082 00083 for(int i = 0; i < 400; i += 2) 00084 if(memory_buf[i] != -1) 00085 printf("ptr:%X, size:%d\n", memory_buf[i], memory_buf[i+1]); 00086 } 00087 00088 static void add_entry(void *x, size_t s, const char* f, const int l) 00089 { 00090 nb_entries++; 00091 for(int i = 0; i < 400; i += 2) 00092 { 00093 if(memory_buf[i] == -1) 00094 { 00095 if(enable) 00096 printf("new ptr:%X, size:%d at %s:%d\n", x, s, f, l); 00097 memory_buf[i] = (int)(x); 00098 memory_buf[i+1] = s; 00099 return; 00100 } 00101 } 00102 if(enable) 00103 printf("No space left in buffer\n"); 00104 } 00105 00106 static void remove_entry(void *x, const char* f, const int l) 00107 { 00108 nb_entries--; 00109 for(int i = 0; i < 400; i += 2) 00110 { 00111 if(memory_buf[i] == (int)(x)) 00112 { 00113 if(enable) 00114 printf("free ptr:%X, size:%d at %s:%d\n", memory_buf[i], memory_buf[i+1], f, l); 00115 memory_buf[i] = -1; 00116 memory_buf[i+1] = 0; 00117 return; 00118 } 00119 } 00120 if(enable) 00121 printf("not found\n"); 00122 } 00123 00124 #ifdef MBED 00125 /** 00126 * gettimeofday() not in mbed 00127 */ 00128 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone) 00129 { 00130 t->tv_sec = time(NULL); 00131 t->tv_usec = 0; /* 1sec precision only */ 00132 } 00133 00134 #endif 00135 00136 #ifdef WIN32 00137 /** 00138 * gettimeofday() not in Win32 00139 */ 00140 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone) 00141 { 00142 #if defined(_WIN32_WCE) 00143 t->tv_sec = time(NULL); 00144 t->tv_usec = 0; /* 1sec precision only */ 00145 #else 00146 struct _timeb timebuffer; 00147 _ftime(&timebuffer); 00148 t->tv_sec = (long)timebuffer.time; 00149 t->tv_usec = 1000 * timebuffer.millitm; /* 1ms precision */ 00150 #endif 00151 } 00152 00153 00154 /** 00155 * strcasecmp() not in Win32 00156 */ 00157 EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2) 00158 { 00159 while (tolower(*s1) == tolower(*s2++)) 00160 { 00161 if (*s1++ == '\0') 00162 { 00163 return 0; 00164 } 00165 } 00166 00167 return *(unsigned char *)s1 - *(unsigned char *)(s2 - 1); 00168 } 00169 00170 00171 EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size) 00172 { 00173 HKEY hKey; 00174 unsigned long datatype; 00175 unsigned long bufferlength = buf_size; 00176 00177 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, 00178 TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"), 00179 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS) 00180 return -1; 00181 00182 RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength); 00183 RegCloseKey(hKey); 00184 return 0; 00185 } 00186 #endif 00187 00188 #undef malloc 00189 #undef realloc 00190 #undef calloc 00191 #undef free 00192 00193 static const char * out_of_mem_str = "out of memory"; 00194 static const char * file_open_str = "Could not open file \"%s\""; 00195 00196 /* 00197 * Some functions that call display some error trace and then call abort(). 00198 * This just makes life much easier on embedded systems, since we're 00199 * suffering major trauma... 00200 */ 00201 EXP_FUNC void * STDCALL ax_malloc(size_t s, const char* f, const int l) 00202 { 00203 if(enable) 00204 printf("malloc\t"); 00205 00206 void *x; 00207 00208 if ((x = malloc(s)) == NULL) 00209 exit_now(out_of_mem_str); 00210 add_entry(x,s, f, l); 00211 print_buf_stats(); 00212 00213 return x; 00214 } 00215 00216 EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s, const char* f, const int l) 00217 { 00218 if(enable) 00219 printf("realloc\t"); 00220 00221 void *x; 00222 00223 if ((x = realloc(y, s)) == NULL) 00224 exit_now(out_of_mem_str); 00225 remove_entry(y, f, l); 00226 add_entry(x,s, f, l); 00227 print_buf_stats(); 00228 return x; 00229 } 00230 00231 EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s, const char* f, const int l) 00232 { 00233 if(enable) 00234 printf("calloc\t"); 00235 void *x; 00236 00237 if ((x = calloc(n, s)) == NULL) { 00238 exit_now(out_of_mem_str); 00239 } 00240 00241 add_entry(x,n*s, f, l); 00242 print_buf_stats(); 00243 return x; 00244 } 00245 00246 EXP_FUNC void STDCALL ax_free(void *y, const char* f, const int l) 00247 { 00248 if(enable) 00249 printf("free\t"); 00250 00251 remove_entry(y, f, l); 00252 print_buf_stats(); 00253 free(y); 00254 } 00255 /* 00256 EXP_FUNC int STDCALL ax_open(const char *pathname, int flags) 00257 { 00258 int x; 00259 00260 if ((x = open(pathname, flags)) < 0) 00261 exit_now(file_open_str, pathname); 00262 00263 return x; 00264 } 00265 */ 00266 00267 /** 00268 * This is a call which will deliberately exit an application, but will 00269 * display some information before dying. 00270 */ 00271 void exit_now(const char *format, ...) 00272 { 00273 va_list argp; 00274 00275 va_start(argp, format); 00276 vfprintf(stderr, format, argp); 00277 va_end(argp); 00278 abort(); 00279 } 00280
Generated on Tue Jul 12 2022 13:16:02 by
