createconf.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * DUMA - Red-Zone memory allocator.
  3. *
  4. * Copyright (C) 2010-2021 Jeffrey H. Johnson <trnsz@pobox.com>
  5. * Copyright (C) 2006 Michael Eddington <meddington@gmail.com>
  6. * Copyright (C) 2002-2021 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
  7. * Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
  8. *
  9. * License: GNU GPL (GNU General Public License, see COPYING-GPL)
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. *
  26. * FILE CONTENTS:
  27. * --------------
  28. * This is a small tool to generate the "duma_config.h" configuration file.
  29. * Its purpose is to allow fixed size memory allocation on stack, which can
  30. * get protected calling page protection functions.
  31. * Also its nice for the user to be able to see the page size.
  32. */
  33. #include <memory.h>
  34. #include <stdarg.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #if defined( __STDC__ ) && ( __STDC_VERSION__ >= 199901L )
  38. #define HAVE_C99
  39. #include <stdint.h>
  40. #endif /* if defined( __STDC__ ) && ( __STDC_VERSION__ >= 199901L ) */
  41. #ifdef _MSC_VER
  42. #include <direct.h>
  43. #endif /* ifdef _MSC_VER */
  44. #ifndef WIN32
  45. #include <fcntl.h>
  46. #include <sys/mman.h>
  47. #include <unistd.h>
  48. #else /* ifndef WIN32 */
  49. #define WIN32_LEAN_AND_MEAN 1
  50. #include <winbase.h>
  51. #include <windows.h>
  52. #ifndef __CYGWIN__
  53. /* Already defined in cygwin headers */
  54. typedef LPVOID caddr_t;
  55. typedef unsigned u_int;
  56. #endif /* ifndef __CYGWIN__ */
  57. #endif /* ifndef WIN32 */
  58. /*
  59. * Retrieve page size
  60. * size_t Page_Size(void)
  61. */
  62. static size_t
  63. Page_Size(void)
  64. {
  65. #if defined( WIN32 )
  66. SYSTEM_INFO SystemInfo;
  67. GetSystemInfo(&SystemInfo);
  68. return ((size_t)SystemInfo.dwPageSize );
  69. #elif defined( _SC_PAGESIZE )
  70. return ((size_t)sysconf(_SC_PAGESIZE));
  71. #elif defined( _SC_PAGE_SIZE )
  72. return ((size_t)sysconf(_SC_PAGE_SIZE));
  73. #else /* if defined( WIN32 ) */
  74. /* extern int getpagesize(); */
  75. return ( getpagesize());
  76. #endif /* if defined( WIN32 ) */
  77. }
  78. int initattr_ok = 0;
  79. void
  80. #ifdef __GNUC__
  81. __attribute(( constructor ))
  82. #endif /* ifdef __GNUC__ */
  83. init_function(void)
  84. {
  85. initattr_ok = 1;
  86. }
  87. typedef struct
  88. {
  89. int id;
  90. unsigned size;
  91. char *type;
  92. int c99;
  93. } DataType_T;
  94. DataType_T sIntTypes[]
  95. = { { 0, sizeof ( unsigned long int ), "unsigned long int",
  96. 0 }
  97. /* Add additional compiler specific types here */
  98. #if defined( HAVE_C99 )
  99. ,
  100. { 1, sizeof ( uint_fast32_t ), "uint_fast32_t",
  101. 1 },
  102. { 2, sizeof ( uint_fast64_t ), "uint_fast64_t",
  103. 1 }
  104. #endif /* if defined( HAVE_C99 ) */
  105. #ifdef _MSC_VER
  106. ,
  107. { 3, sizeof ( unsigned __int64 ), "unsigned __int64",
  108. 0 }
  109. #endif /* ifdef _MSC_VER */
  110. #ifdef __GNUC__
  111. ,
  112. { 4, sizeof ( unsigned long long int ), "unsigned long long int",
  113. 0 }
  114. #endif /* ifdef __GNUC__ */
  115. };
  116. /* Test access to each data type */
  117. void
  118. testAlignment(int addrIdx, char *buffer, unsigned alignment,
  119. unsigned max_sizeof)
  120. {
  121. unsigned offset;
  122. switch (sIntTypes[addrIdx].id)
  123. {
  124. case 0:
  125. #define TYPE long int
  126. for (offset = 0; offset < max_sizeof; ++offset)
  127. {
  128. TYPE addr = (TYPE)( buffer ) + offset;
  129. if (addr % alignment == 0)
  130. {
  131. *((unsigned char *)addr ) = 0;
  132. *((unsigned short int *)addr ) = 0;
  133. *((unsigned int *)addr ) = 0;
  134. *((unsigned long int *)addr ) = 0L;
  135. *((float *)addr ) = 0.0F;
  136. *((double *)addr ) = 0.0;
  137. *((long double *)addr ) = 0.0;
  138. #if defined( HAVE_C99 )
  139. *((int_fast32_t *)addr ) = 0;
  140. *((int_fast64_t *)addr ) = 0;
  141. #endif /* if defined( HAVE_C99 ) */
  142. #ifdef _MSC_VER
  143. *((unsigned __int64 *)addr ) = 0L;
  144. #endif /* ifdef _MSC_VER */
  145. #ifdef __GNUC__
  146. *((unsigned long long int *)addr ) = 0L;
  147. #endif /* ifdef __GNUC__ */
  148. }
  149. }
  150. break;
  151. #undef TYPE
  152. #if defined( HAVE_C99 )
  153. case 1:
  154. #define TYPE int_fast32_t
  155. for (offset = 0; offset < max_sizeof; ++offset)
  156. {
  157. TYPE addr = (TYPE)( buffer ) + offset;
  158. if (addr % alignment == 0)
  159. {
  160. *((unsigned char *)addr ) = 0;
  161. *((unsigned short int *)addr ) = 0;
  162. *((unsigned int *)addr ) = 0;
  163. *((unsigned long int *)addr ) = 0L;
  164. *((float *)addr ) = 0.0F;
  165. *((double *)addr ) = 0.0;
  166. *((long double *)addr ) = 0.0;
  167. #if defined( HAVE_C99 )
  168. *((int_fast32_t *)addr ) = 0;
  169. *((int_fast64_t *)addr ) = 0;
  170. #endif /* if defined( HAVE_C99 ) */
  171. #ifdef _MSC_VER
  172. *((unsigned __int64 *)addr ) = 0L;
  173. #endif /* ifdef _MSC_VER */
  174. #ifdef __GNUC__
  175. *((unsigned long long int *)addr ) = 0L;
  176. #endif /* ifdef __GNUC__ */
  177. }
  178. }
  179. break;
  180. #undef TYPE
  181. case 2:
  182. #define TYPE int_fast64_t
  183. for (offset = 0; offset < max_sizeof; ++offset)
  184. {
  185. TYPE addr = (TYPE)( buffer ) + offset;
  186. if (addr % alignment == 0)
  187. {
  188. *((unsigned char *)addr ) = 0;
  189. *((unsigned short int *)addr ) = 0;
  190. *((unsigned int *)addr ) = 0;
  191. *((unsigned long int *)addr ) = 0L;
  192. *((float *)addr ) = 0.0F;
  193. *((double *)addr ) = 0.0;
  194. *((long double *)addr ) = 0.0;
  195. #if defined( HAVE_C99 )
  196. *((int_fast32_t *)addr ) = 0;
  197. *((int_fast64_t *)addr ) = 0;
  198. #endif /* if defined( HAVE_C99 ) */
  199. #ifdef _MSC_VER
  200. *((unsigned __int64 *)addr ) = 0L;
  201. #endif /* ifdef _MSC_VER */
  202. #ifdef __GNUC__
  203. *((unsigned long long int *)addr ) = 0L;
  204. #endif /* ifdef __GNUC__ */
  205. }
  206. }
  207. break;
  208. #undef TYPE
  209. #endif /* if defined( HAVE_C99 ) */
  210. #ifdef _MSC_VER
  211. case 3:
  212. #undef TYPE
  213. #define TYPE unsigned __int64
  214. for (offset = 0; offset < max_sizeof; ++offset)
  215. {
  216. TYPE addr = (TYPE)( buffer ) + offset;
  217. if (addr % alignment == 0)
  218. {
  219. *((unsigned char *)addr ) = 0;
  220. *((unsigned short int *)addr ) = 0;
  221. *((unsigned int *)addr ) = 0;
  222. *((unsigned long int *)addr ) = 0L;
  223. *((float *)addr ) = 0.0F;
  224. *((double *)addr ) = 0.0;
  225. *((long double *)addr ) = 0.0;
  226. #if defined( HAVE_C99 )
  227. *((int_fast32_t *)addr ) = 0;
  228. *((int_fast64_t *)addr ) = 0;
  229. #endif /* if defined( HAVE_C99 ) */
  230. #ifdef _MSC_VER
  231. *((unsigned __int64 *)addr ) = 0L;
  232. #endif /* ifdef _MSC_VER */
  233. #ifdef __GNUC__
  234. *((unsigned long long int *)addr ) = 0L;
  235. #endif /* ifdef __GNUC__ */
  236. }
  237. }
  238. break;
  239. #undef TYPE
  240. #endif /* ifdef _MSC_VER */
  241. #ifdef __GNUC__
  242. case 4:
  243. #define TYPE unsigned long long int
  244. for (offset = 0; offset < max_sizeof; ++offset)
  245. {
  246. TYPE addr = (TYPE)( buffer ) + offset;
  247. if (addr % alignment == 0)
  248. {
  249. *((unsigned char *)addr ) = 0;
  250. *((unsigned short int *)addr ) = 0;
  251. *((unsigned int *)addr ) = 0;
  252. *((unsigned long int *)addr ) = 0L;
  253. *((float *)addr ) = 0.0F;
  254. *((double *)addr ) = 0.0;
  255. *((long double *)addr ) = 0.0;
  256. #if defined( HAVE_C99 )
  257. *((int_fast32_t *)addr ) = 0;
  258. *((int_fast64_t *)addr ) = 0;
  259. #endif /* if defined( HAVE_C99 ) */
  260. #ifdef _MSC_VER
  261. *((unsigned __int64 *)addr ) = 0L;
  262. #endif /* ifdef _MSC_VER */
  263. #ifdef __GNUC__
  264. *((unsigned long long int *)addr ) = 0L;
  265. #endif /* ifdef __GNUC__ */
  266. }
  267. }
  268. break;
  269. #undef TYPE
  270. #endif /* ifdef __GNUC__ */
  271. ;
  272. }
  273. }
  274. void
  275. writeFile(const char *filename, unsigned long pagesize, int addrIdx,
  276. int sizeIdx, unsigned alignment, int malloc0strategy)
  277. {
  278. FILE *f = fopen(filename, "w");
  279. if (!f)
  280. {
  281. fprintf(
  282. stderr,
  283. "createconf: Error: could not open file '%s'\n",
  284. filename);
  285. return;
  286. }
  287. fprintf(f, "/*\n");
  288. fprintf(f, " * WARNING: DO NOT CHANGE THIS FILE!\n");
  289. fprintf(f, " * This file is automatically generated from createconf\n");
  290. #if defined( WIN32 )
  291. #if defined( _MSC_VER )
  292. fprintf(
  293. f,
  294. " * using Microsoft Visual C++ under MS Windows(TM) on %s\n",
  295. __DATE__);
  296. #else /* if defined( _MSC_VER ) */
  297. fprintf(f, " * under MS Windows(TM) on %s\n", __DATE__);
  298. #endif /* if defined( _MSC_VER ) */
  299. #elif ( defined( sgi ))
  300. fprintf(f, " * under sgi on %s\n", __DATE__);
  301. #elif ( defined( _AIX ))
  302. fprintf(f, " * under AIX on %s\n", __DATE__);
  303. #elif ( defined( __linux__ ))
  304. fprintf(f, " * under Linux on %s\n", __DATE__);
  305. #else /* if defined( WIN32 ) */
  306. fprintf(f, " * on %s \n", __DATE__);
  307. #endif /* if defined( WIN32 ) */
  308. fprintf(f, " */\n");
  309. fprintf(f, "\n");
  310. fprintf(f, "#ifndef _DUMA_CONFIG_H_\n");
  311. fprintf(f, "#define _DUMA_CONFIG_H_\n");
  312. fprintf(f, "\n");
  313. fprintf(f, "/*\n");
  314. fprintf(f, " * Configuration of DUMA:\n");
  315. fprintf(f, " */\n");
  316. /* DUMA_SO_LIBRARY */
  317. fprintf(f, "#ifdef DUMA_SO_LIBRARY\n");
  318. fprintf(f, "\n");
  319. fprintf(f, "#ifdef DUMA_NO_GLOBAL_MALLOC_FREE\n");
  320. fprintf(f, "#undef DUMA_NO_GLOBAL_MALLOC_FREE\n");
  321. fprintf(f, "#endif\n\n");
  322. fprintf(f, "#ifdef DUMA_EXPLICIT_INIT\n");
  323. fprintf(f, "#undef DUMA_EXPLICIT_INIT\n");
  324. fprintf(f, "#endif\n\n");
  325. fprintf(f, "#ifdef DUMA_NO_THREAD_SAFETY\n");
  326. fprintf(f, "#undef DUMA_NO_THREAD_SAFETY\n");
  327. fprintf(f, "#endif\n\n");
  328. #ifndef DUMA_SEMAPHORES
  329. fprintf(f, "#ifdef DUMA_SEMAPHORES\n");
  330. fprintf(f, "#undef DUMA_SEMAPHORES\n");
  331. fprintf(f, "#endif\n\n");
  332. #else /* ifndef DUMA_SEMAPHORES */
  333. fprintf(f, "#ifndef DUMA_SEMAPHORES\n");
  334. fprintf(f, "#define DUMA_SEMAPHORES\n");
  335. fprintf(f, "#endif\n\n");
  336. #endif /* ifndef DUMA_SEMAPHORES */
  337. #ifndef DUMA_SO_NO_CPP_SUPPORT
  338. fprintf(f, "#ifdef DUMA_NO_CPP_SUPPORT\n");
  339. fprintf(f, "#undef DUMA_NO_CPP_SUPPORT\n");
  340. fprintf(f, "#endif\n\n");
  341. #else /* ifndef DUMA_SO_NO_CPP_SUPPORT */
  342. fprintf(f, "#ifndef DUMA_NO_CPP_SUPPORT\n");
  343. fprintf(f, "#define DUMA_NO_CPP_SUPPORT\n");
  344. fprintf(f, "#endif\n\n");
  345. #endif /* ifndef DUMA_SO_NO_CPP_SUPPORT */
  346. #ifndef DUMA_SO_NO_LEAKDETECTION
  347. fprintf(f, "#ifdef DUMA_NO_LEAKDETECTION\n");
  348. fprintf(f, "#undef DUMA_NO_LEAKDETECTION\n");
  349. fprintf(f, "#endif\n\n");
  350. #else /* ifndef DUMA_SO_NO_LEAKDETECTION */
  351. fprintf(f, "#ifndef DUMA_NO_LEAKDETECTION\n");
  352. fprintf(f, "#define DUMA_NO_LEAKDETECTION\n");
  353. fprintf(f, "#endif\n\n");
  354. #endif /* ifndef DUMA_SO_NO_LEAKDETECTION */
  355. #ifndef DUMA_SO_PREFER_ATEXIT
  356. fprintf(f, "#ifdef DUMA_PREFER_ATEXIT\n");
  357. fprintf(f, "#undef DUMA_PREFER_ATEXIT\n");
  358. fprintf(f, "#endif\n\n");
  359. #else /* ifndef DUMA_SO_PREFER_ATEXIT */
  360. fprintf(f, "#ifndef DUMA_PREFER_ATEXIT\n");
  361. fprintf(f, "#define DUMA_PREFER_ATEXIT\n");
  362. fprintf(f, "#endif\n\n");
  363. #endif /* ifndef DUMA_SO_PREFER_ATEXIT */
  364. #ifndef DUMA_SO_PREFER_GETENV
  365. fprintf(f, "#ifdef DUMA_PREFER_GETENV\n");
  366. fprintf(f, "#undef DUMA_PREFER_GETENV\n");
  367. fprintf(f, "#endif\n\n");
  368. #else /* ifndef DUMA_SO_PREFER_GETENV */
  369. fprintf(f, "#ifndef DUMA_PREFER_GETENV\n");
  370. fprintf(f, "#define DUMA_PREFER_GETENV\n");
  371. fprintf(f, "#endif\n\n");
  372. #endif /* ifndef DUMA_SO_PREFER_GETENV */
  373. fprintf(f, "#ifdef DUMA_OLD_NEW_MACRO\n");
  374. fprintf(f, "#undef DUMA_OLD_NEW_MACRO\n");
  375. fprintf(f, "#endif\n\n");
  376. fprintf(f, "#ifdef DUMA_OLD_DEL_MACRO\n");
  377. fprintf(f, "#undef DUMA_OLD_DEL_MACRO\n");
  378. fprintf(f, "#endif\n\n");
  379. #ifndef DUMA_NO_STRERROR
  380. fprintf(f, "#ifdef DUMA_NO_STRERROR\n");
  381. fprintf(f, "#undef DUMA_NO_STRERROR\n");
  382. fprintf(f, "#endif\n\n");
  383. #else /* ifndef DUMA_NO_STRERROR */
  384. fprintf(f, "#ifndef DUMA_NO_STRERROR\n");
  385. fprintf(f, "#define DUMA_NO_STRERROR\n");
  386. fprintf(f, "#endif\n\n");
  387. #endif /* ifndef DUMA_NO_STRERROR */
  388. #ifndef DUMA_SO_NO_HANG_MSG
  389. fprintf(f, "#ifdef DUMA_NO_HANG_MSG\n");
  390. fprintf(f, "#undef DUMA_NO_HANG_MSG\n");
  391. fprintf(f, "#endif\n\n");
  392. #else /* ifndef DUMA_SO_NO_HANG_MSG */
  393. fprintf(f, "#ifndef DUMA_NO_HANG_MSG\n");
  394. fprintf(f, "#define DUMA_NO_HANG_MSG\n");
  395. fprintf(f, "#endif\n\n");
  396. #endif /* ifndef DUMA_SO_NO_HANG_MSG */
  397. /* DUMA_DLL_LIBRARY */
  398. fprintf(f, "#elif defined(DUMA_DLL_LIBRARY)\n");
  399. fprintf(f, "\n");
  400. fprintf(f, "#define DUMA_SKIP_SETUP 1\n");
  401. fprintf(f, "#define DUMA_NO_GLOBAL_MALLOC_FREE 1\n");
  402. fprintf(f, "#define DUMA_EXPLICIT_INIT 1\n");
  403. fprintf(f, "#ifdef DUMA_NO_THREAD_SAFETY\n");
  404. fprintf(f, "#undef DUMA_NO_THREAD_SAFETY\n");
  405. fprintf(f, "#endif\n\n");
  406. #ifndef DUMA_SO_NO_CPP_SUPPORT
  407. fprintf(f, "#ifdef DUMA_NO_CPP_SUPPORT\n");
  408. fprintf(f, "#undef DUMA_NO_CPP_SUPPORT\n");
  409. fprintf(f, "#endif\n\n");
  410. #else /* ifndef DUMA_SO_NO_CPP_SUPPORT */
  411. fprintf(f, "#ifndef DUMA_NO_CPP_SUPPORT\n");
  412. fprintf(f, "#define DUMA_NO_CPP_SUPPORT\n");
  413. fprintf(f, "#endif\n\n");
  414. #endif /* ifndef DUMA_SO_NO_CPP_SUPPORT */
  415. #ifndef DUMA_SO_NO_LEAKDETECTION
  416. fprintf(f, "#ifdef DUMA_NO_LEAKDETECTION\n");
  417. fprintf(f, "#undef DUMA_NO_LEAKDETECTION\n");
  418. fprintf(f, "#endif\n\n");
  419. #else /* ifndef DUMA_SO_NO_LEAKDETECTION */
  420. fprintf(f, "#ifndef DUMA_NO_LEAKDETECTION\n");
  421. fprintf(f, "#define DUMA_NO_LEAKDETECTION\n");
  422. fprintf(f, "#endif\n\n");
  423. #endif /* ifndef DUMA_SO_NO_LEAKDETECTION */
  424. #ifndef DUMA_SO_PREFER_ATEXIT
  425. fprintf(f, "#ifdef DUMA_PREFER_ATEXIT\n");
  426. fprintf(f, "#undef DUMA_PREFER_ATEXIT\n");
  427. fprintf(f, "#endif\n\n");
  428. #else /* ifndef DUMA_SO_PREFER_ATEXIT */
  429. fprintf(f, "#ifndef DUMA_PREFER_ATEXIT\n");
  430. fprintf(f, "#define DUMA_PREFER_ATEXIT\n");
  431. fprintf(f, "#endif\n\n");
  432. #endif /* ifndef DUMA_SO_PREFER_ATEXIT */
  433. #ifndef DUMA_SO_PREFER_GETENV
  434. fprintf(f, "#ifdef DUMA_PREFER_GETENV\n");
  435. fprintf(f, "#undef DUMA_PREFER_GETENV\n");
  436. fprintf(f, "#endif\n\n");
  437. #else /* ifndef DUMA_SO_PREFER_GETENV */
  438. fprintf(f, "#ifndef DUMA_PREFER_GETENV\n");
  439. fprintf(f, "#define DUMA_PREFER_GETENV\n");
  440. fprintf(f, "#endif\n\n");
  441. #endif /* ifndef DUMA_SO_PREFER_GETENV */
  442. fprintf(f, "#ifdef DUMA_OLD_NEW_MACRO\n");
  443. fprintf(f, "#undef DUMA_OLD_NEW_MACRO\n");
  444. fprintf(f, "#endif\n\n");
  445. fprintf(f, "#ifdef DUMA_OLD_DEL_MACRO\n");
  446. fprintf(f, "#undef DUMA_OLD_DEL_MACRO\n");
  447. fprintf(f, "#endif\n\n");
  448. #ifndef DUMA_SO_NO_HANG_MSG
  449. fprintf(f, "#ifdef DUMA_NO_HANG_MSG\n");
  450. fprintf(f, "#undef DUMA_NO_HANG_MSG\n");
  451. fprintf(f, "#endif\n\n");
  452. #else /* ifndef DUMA_SO_NO_HANG_MSG */
  453. fprintf(f, "#ifndef DUMA_NO_HANG_MSG\n");
  454. fprintf(f, "#define DUMA_NO_HANG_MSG\n");
  455. fprintf(f, "#endif\n\n");
  456. #endif /* ifndef DUMA_SO_NO_HANG_MSG */
  457. /* DUMA_DETOURS */
  458. fprintf(f, "#elif defined(DUMA_DETOURS)\n");
  459. fprintf(f, "\n");
  460. fprintf(f, "#define DUMA_SKIP_SETUP 1\n");
  461. fprintf(f, "#define DUMA_NO_GLOBAL_MALLOC_FREE 1\n");
  462. fprintf(f, "#define DUMA_EXPLICIT_INIT 1\n");
  463. fprintf(f, "#ifdef DUMA_NO_THREAD_SAFETY\n");
  464. fprintf(f, "#undef DUMA_NO_THREAD_SAFETY\n");
  465. fprintf(f, "#endif\n\n");
  466. #ifndef DUMA_SO_NO_CPP_SUPPORT
  467. fprintf(f, "#ifdef DUMA_NO_CPP_SUPPORT\n");
  468. fprintf(f, "#undef DUMA_NO_CPP_SUPPORT\n");
  469. fprintf(f, "#endif\n\n");
  470. #else /* ifndef DUMA_SO_NO_CPP_SUPPORT */
  471. fprintf(f, "#ifndef DUMA_NO_CPP_SUPPORT\n");
  472. fprintf(f, "#define DUMA_NO_CPP_SUPPORT\n");
  473. fprintf(f, "#endif\n\n");
  474. #endif /* ifndef DUMA_SO_NO_CPP_SUPPORT */
  475. #ifndef DUMA_SO_NO_LEAKDETECTION
  476. fprintf(f, "#ifdef DUMA_NO_LEAKDETECTION\n");
  477. fprintf(f, "#undef DUMA_NO_LEAKDETECTION\n");
  478. fprintf(f, "#endif\n\n");
  479. #else /* ifndef DUMA_SO_NO_LEAKDETECTION */
  480. fprintf(f, "#ifndef DUMA_NO_LEAKDETECTION\n");
  481. fprintf(f, "#define DUMA_NO_LEAKDETECTION\n");
  482. fprintf(f, "#endif\n\n");
  483. #endif /* ifndef DUMA_SO_NO_LEAKDETECTION */
  484. #ifndef DUMA_SO_PREFER_ATEXIT
  485. fprintf(f, "#ifdef DUMA_PREFER_ATEXIT\n");
  486. fprintf(f, "#undef DUMA_PREFER_ATEXIT\n");
  487. fprintf(f, "#endif\n\n");
  488. #else /* ifndef DUMA_SO_PREFER_ATEXIT */
  489. fprintf(f, "#ifndef DUMA_PREFER_ATEXIT\n");
  490. fprintf(f, "#define DUMA_PREFER_ATEXIT\n");
  491. fprintf(f, "#endif\n\n");
  492. #endif /* ifndef DUMA_SO_PREFER_ATEXIT */
  493. #ifndef DUMA_SO_PREFER_GETENV
  494. fprintf(f, "#ifdef DUMA_PREFER_GETENV\n");
  495. fprintf(f, "#undef DUMA_PREFER_GETENV\n");
  496. fprintf(f, "#endif\n\n");
  497. #else /* ifndef DUMA_SO_PREFER_GETENV */
  498. fprintf(f, "#ifndef DUMA_PREFER_GETENV\n");
  499. fprintf(f, "#define DUMA_PREFER_GETENV\n");
  500. fprintf(f, "#endif\n\n");
  501. #endif /* ifndef DUMA_SO_PREFER_GETENV */
  502. fprintf(f, "#ifdef DUMA_OLD_NEW_MACRO\n");
  503. fprintf(f, "#undef DUMA_OLD_NEW_MACRO\n");
  504. fprintf(f, "#endif\n\n");
  505. fprintf(f, "#ifdef DUMA_OLD_DEL_MACRO\n");
  506. fprintf(f, "#undef DUMA_OLD_DEL_MACRO\n");
  507. fprintf(f, "#endif\n\n");
  508. #ifndef DUMA_SO_NO_HANG_MSG
  509. fprintf(f, "#ifdef DUMA_NO_HANG_MSG\n");
  510. fprintf(f, "#undef DUMA_NO_HANG_MSG\n");
  511. fprintf(f, "#endif\n\n");
  512. #else /* ifndef DUMA_SO_NO_HANG_MSG */
  513. fprintf(f, "#ifndef DUMA_NO_HANG_MSG\n");
  514. fprintf(f, "#define DUMA_NO_HANG_MSG\n");
  515. fprintf(f, "#endif\n\n");
  516. #endif /* ifndef DUMA_SO_NO_HANG_MSG */
  517. /* REGULAR CONFIG */
  518. fprintf(f, "#else\n");
  519. fprintf(f, "\n");
  520. #ifndef DUMA_NO_GLOBAL_MALLOC_FREE
  521. fprintf(f, "#ifdef DUMA_NO_GLOBAL_MALLOC_FREE\n");
  522. fprintf(f, "#undef DUMA_NO_GLOBAL_MALLOC_FREE\n");
  523. fprintf(f, "#endif\n\n");
  524. #else /* ifndef DUMA_NO_GLOBAL_MALLOC_FREE */
  525. fprintf(f, "#ifndef DUMA_NO_GLOBAL_MALLOC_FREE\n");
  526. fprintf(f, "#define DUMA_NO_GLOBAL_MALLOC_FREE\n");
  527. fprintf(f, "#endif\n\n");
  528. #endif /* ifndef DUMA_NO_GLOBAL_MALLOC_FREE */
  529. #ifndef DUMA_EXPLICIT_INIT
  530. fprintf(f, "#ifdef DUMA_EXPLICIT_INIT\n");
  531. fprintf(f, "#undef DUMA_EXPLICIT_INIT\n");
  532. fprintf(f, "#endif\n\n");
  533. #else /* ifndef DUMA_EXPLICIT_INIT */
  534. fprintf(f, "#ifndef DUMA_EXPLICIT_INIT\n");
  535. fprintf(f, "#define DUMA_EXPLICIT_INIT\n");
  536. fprintf(f, "#endif\n\n");
  537. #endif /* ifndef DUMA_EXPLICIT_INIT */
  538. #ifndef DUMA_NO_THREAD_SAFETY
  539. fprintf(f, "#ifdef DUMA_NO_THREAD_SAFETY\n");
  540. fprintf(f, "#undef DUMA_NO_THREAD_SAFETY\n");
  541. fprintf(f, "#endif\n\n");
  542. #else /* ifndef DUMA_NO_THREAD_SAFETY */
  543. fprintf(f, "#ifndef DUMA_NO_THREAD_SAFETY\n");
  544. fprintf(f, "#define DUMA_NO_THREAD_SAFETY\n");
  545. fprintf(f, "#endif\n\n");
  546. #endif /* ifndef DUMA_NO_THREAD_SAFETY */
  547. #ifndef DUMA_LIB_NO_CPP_SUPPORT
  548. fprintf(f, "#ifdef DUMA_NO_CPP_SUPPORT\n");
  549. fprintf(f, "#undef DUMA_NO_CPP_SUPPORT\n");
  550. fprintf(f, "#endif\n\n");
  551. #else /* ifndef DUMA_LIB_NO_CPP_SUPPORT */
  552. fprintf(f, "#ifndef DUMA_NO_CPP_SUPPORT\n");
  553. fprintf(f, "#define DUMA_NO_CPP_SUPPORT\n");
  554. fprintf(f, "#endif\n\n");
  555. #endif /* ifndef DUMA_LIB_NO_CPP_SUPPORT */
  556. #ifndef DUMA_LIB_NO_LEAKDETECTION
  557. fprintf(f, "#ifdef DUMA_NO_LEAKDETECTION\n");
  558. fprintf(f, "#undef DUMA_NO_LEAKDETECTION\n");
  559. fprintf(f, "#endif\n\n");
  560. #else /* ifndef DUMA_LIB_NO_LEAKDETECTION */
  561. fprintf(f, "#ifndef DUMA_NO_LEAKDETECTION\n");
  562. fprintf(f, "#define DUMA_NO_LEAKDETECTION\n");
  563. fprintf(f, "#endif\n\n");
  564. #endif /* ifndef DUMA_LIB_NO_LEAKDETECTION */
  565. #ifndef DUMA_LIB_PREFER_ATEXIT
  566. fprintf(f, "#ifdef DUMA_PREFER_ATEXIT\n");
  567. fprintf(f, "#undef DUMA_PREFER_ATEXIT\n");
  568. fprintf(f, "#endif\n\n");
  569. #else /* ifndef DUMA_LIB_PREFER_ATEXIT */
  570. fprintf(f, "#ifndef DUMA_PREFER_ATEXIT\n");
  571. fprintf(f, "#define DUMA_PREFER_ATEXIT\n");
  572. fprintf(f, "#endif\n\n");
  573. #endif /* ifndef DUMA_LIB_PREFER_ATEXIT */
  574. #ifndef DUMA_LIB_PREFER_GETENV
  575. fprintf(f, "#ifdef DUMA_PREFER_GETENV\n");
  576. fprintf(f, "#undef DUMA_PREFER_GETENV\n");
  577. fprintf(f, "#endif\n\n");
  578. #else /* ifndef DUMA_LIB_PREFER_GETENV */
  579. fprintf(f, "#ifndef DUMA_PREFER_GETENV\n");
  580. fprintf(f, "#define DUMA_PREFER_GETENV\n");
  581. fprintf(f, "#endif\n\n");
  582. #endif /* ifndef DUMA_LIB_PREFER_GETENV */
  583. #ifndef DUMA_OLD_NEW_MACRO
  584. fprintf(f, "#ifdef DUMA_OLD_NEW_MACRO\n");
  585. fprintf(f, "#undef DUMA_OLD_NEW_MACRO\n");
  586. fprintf(f, "#endif\n\n");
  587. #else /* ifndef DUMA_OLD_NEW_MACRO */
  588. fprintf(f, "#ifndef DUMA_OLD_NEW_MACRO\n");
  589. fprintf(f, "#define DUMA_OLD_NEW_MACRO\n");
  590. fprintf(f, "#endif\n\n");
  591. #endif /* ifndef DUMA_OLD_NEW_MACRO */
  592. #ifndef DUMA_OLD_DEL_MACRO
  593. fprintf(f, "#ifdef DUMA_OLD_DEL_MACRO\n");
  594. fprintf(f, "#undef DUMA_OLD_DEL_MACRO\n");
  595. fprintf(f, "#endif\n\n");
  596. #else /* ifndef DUMA_OLD_DEL_MACRO */
  597. fprintf(f, "#ifndef DUMA_OLD_DEL_MACRO\n");
  598. fprintf(f, "#define DUMA_OLD_DEL_MACRO\n");
  599. fprintf(f, "#endif\n\n");
  600. #endif /* ifndef DUMA_OLD_DEL_MACRO */
  601. #ifndef DUMA_NO_STRERROR
  602. fprintf(f, "#ifdef DUMA_NO_STRERROR\n");
  603. fprintf(f, "#undef DUMA_NO_STRERROR\n");
  604. fprintf(f, "#endif\n\n");
  605. #else /* ifndef DUMA_NO_STRERROR */
  606. fprintf(f, "#ifndef DUMA_NO_STRERROR\n");
  607. fprintf(f, "#define DUMA_NO_STRERROR\n");
  608. fprintf(f, "#endif\n\n");
  609. #endif /* ifndef DUMA_NO_STRERROR */
  610. #ifndef DUMA_LIB_NO_HANG_MSG
  611. fprintf(f, "#ifdef DUMA_NO_HANG_MSG\n");
  612. fprintf(f, "#undef DUMA_NO_HANG_MSG\n");
  613. fprintf(f, "#endif\n\n");
  614. #else /* ifndef DUMA_LIB_NO_HANG_MSG */
  615. fprintf(f, "#ifndef DUMA_NO_HANG_MSG\n");
  616. fprintf(f, "#define DUMA_NO_HANG_MSG\n");
  617. fprintf(f, "#endif\n\n");
  618. #endif /* ifndef DUMA_LIB_NO_HANG_MSG */
  619. fprintf(f, "#endif\n");
  620. fprintf(f, "\n");
  621. fprintf(f, "\n");
  622. if (addrIdx >= 0 && sIntTypes[addrIdx].c99)
  623. {
  624. fprintf(f, "#include <stdint.h>\n\n");
  625. }
  626. fprintf(f, "/*\n");
  627. fprintf(f,
  628. " * Number of bytes per virtual-memory page, as returned by "
  629. "Page_Size().\n");
  630. fprintf(f, " */\n");
  631. fprintf(f, "#define DUMA_PAGE_SIZE %lu\n", pagesize);
  632. fprintf(f, "\n");
  633. fprintf(f, "/*\n");
  634. fprintf(f, " * Minimum required alignment by CPU.\n");
  635. fprintf(f, " */\n");
  636. fprintf(f, "#define DUMA_MIN_ALIGNMENT %d\n", alignment);
  637. fprintf(f, "\n");
  638. fprintf(f, "/*\n");
  639. fprintf(f,
  640. " * Build environment supports the '__attribute ((constructor))'?\n");
  641. fprintf(f, " */\n");
  642. if (initattr_ok)
  643. {
  644. fprintf(f, "#define DUMA_GNU_INIT_ATTR 1\n");
  645. }
  646. else
  647. {
  648. fprintf(f, "/* #define DUMA_GNU_INIT_ATTR 0 */\n");
  649. }
  650. fprintf(f, "\n");
  651. fprintf(f, "/*\n");
  652. fprintf(f, " * An integer type with same size as 'void *'\n");
  653. fprintf(f, " */\n");
  654. if (addrIdx >= 0)
  655. {
  656. fprintf(f, "typedef %s DUMA_ADDR;\n", sIntTypes[addrIdx].type);
  657. }
  658. else
  659. {
  660. fprintf(f, "/* Error: No datatype for DUMA_ADDR found! */\n");
  661. }
  662. fprintf(f, "\n");
  663. fprintf(f, "/*\n");
  664. fprintf(f, " * An integer type with same size as 'size_t'\n");
  665. fprintf(f, " */\n");
  666. if (sizeIdx >= 0)
  667. {
  668. fprintf(f, "typedef %s DUMA_SIZE;\n", sIntTypes[sizeIdx].type);
  669. }
  670. else
  671. {
  672. fprintf(f, "/* No datatype for DUMA_SIZE found! */\n");
  673. }
  674. fprintf(f, "\n");
  675. fprintf(f, "/*\n");
  676. fprintf(f, " * Default behaviour on malloc(0).\n");
  677. fprintf(f, " */\n");
  678. fprintf(f,
  679. "#define DUMA_DEFAULT_MALLOC_0_STRATEGY %d\n",
  680. malloc0strategy);
  681. fprintf(f, "\n");
  682. fprintf(f, "#endif /* _DUMA_CONFIG_H_ */\n");
  683. fclose(f);
  684. }
  685. #ifdef __cplusplus
  686. #define DUMA_EXTERN_C extern "C"
  687. #else /* ifdef __cplusplus */
  688. #define DUMA_EXTERN_C extern
  689. #endif /* ifdef __cplusplus */
  690. int
  691. main()
  692. {
  693. int iNumIntTypes, iIt;
  694. int addrIdx, sizeIdx;
  695. unsigned alignment;
  696. unsigned max_sizeof;
  697. int malloc0strategy;
  698. char buffer[1024];
  699. char filename[1024];
  700. unsigned long pagesize = Page_Size();
  701. #if defined( __MINGW32__ ) || defined( __MINGW64__ )
  702. fprintf(stderr, "createconf: Platform is MINGW\n");
  703. #endif /* if defined( __MINGW32__ ) || defined( __MINGW64__ ) */
  704. #if defined( __CYGWIN__ )
  705. fprintf(stderr, "createconf: Platform is CYGWIN\n");
  706. #endif /* if defined( __CYGWIN__ ) */
  707. #if defined( WIN32 )
  708. fprintf(stderr, "createconf: WIN32 is set\n");
  709. #endif /* if defined( WIN32 ) */
  710. #if defined( _MSC_VER )
  711. fprintf(stderr, "createconf: Compiler is MS Visual C++ (_MSC_VER set)\n");
  712. #endif /* if defined( _MSC_VER ) */
  713. iNumIntTypes = sizeof ( sIntTypes ) / sizeof ( sIntTypes[0] );
  714. /* Detect compatible type for ADDRESS */
  715. addrIdx = -1;
  716. for (iIt = 0; iIt < iNumIntTypes; ++iIt)
  717. {
  718. if (sIntTypes[iIt].size >= sizeof ( void * ))
  719. {
  720. addrIdx = iIt;
  721. break;
  722. }
  723. }
  724. /* Detect compatible type for SIZE_T */
  725. sizeIdx = -1;
  726. for (iIt = 0; iIt < iNumIntTypes; ++iIt)
  727. {
  728. if (sIntTypes[iIt].size >= sizeof ( size_t ))
  729. {
  730. sizeIdx = iIt;
  731. break;
  732. }
  733. }
  734. /* Detect maximum data type, which should be maximum alignment */
  735. max_sizeof = 0;
  736. for (iIt = 0; iIt < iNumIntTypes; ++iIt)
  737. {
  738. if (max_sizeof < sIntTypes[iIt].size)
  739. {
  740. max_sizeof = sIntTypes[iIt].size;
  741. }
  742. }
  743. if (max_sizeof < (int)sizeof ( float ))
  744. {
  745. max_sizeof = sizeof ( float );
  746. }
  747. if (max_sizeof < (int)sizeof ( double ))
  748. {
  749. max_sizeof = sizeof ( double );
  750. }
  751. if (max_sizeof < (int)sizeof ( long double ))
  752. {
  753. max_sizeof = sizeof ( long double );
  754. }
  755. /* test for behaviour on malloc(0) */
  756. {
  757. char *pcNullPtrA = (char *)malloc(0);
  758. char *pcNullPtrB = (char *)malloc(0);
  759. if (!pcNullPtrA)
  760. {
  761. malloc0strategy = 1;
  762. }
  763. else if (pcNullPtrA == pcNullPtrB)
  764. {
  765. malloc0strategy = 2;
  766. }
  767. else
  768. {
  769. malloc0strategy = 3;
  770. }
  771. }
  772. #ifdef _MSC_VER
  773. {
  774. /* fix output path when started form Visual C++ IDE */
  775. char directory[1024];
  776. char *start;
  777. _getcwd(directory, 1024); /* get current directory */
  778. if (( start = strstr(directory, "win32-msvc")))
  779. {
  780. *start = 0;
  781. strcpy(filename, directory);
  782. strcat(filename, "duma_config.h");
  783. }
  784. else
  785. {
  786. strcpy(filename, "duma_config.h");
  787. }
  788. }
  789. #else /* ifdef _MSC_VER */
  790. strcpy(filename, "duma_config.h");
  791. #endif /* ifdef _MSC_VER */
  792. /* detect minimum alignment */
  793. alignment = max_sizeof;
  794. do
  795. {
  796. /* do the alignment access tests */
  797. testAlignment(addrIdx, buffer, alignment, max_sizeof);
  798. /* write whole config file. next test may crash ! */
  799. writeFile(
  800. filename,
  801. pagesize,
  802. addrIdx,
  803. sizeIdx,
  804. alignment,
  805. malloc0strategy);
  806. /* try next lower alignment */
  807. alignment >>= 1;
  808. }
  809. while ( alignment > 0 );
  810. return ( 0 );
  811. }