createconf.c 21 KB

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