dipto181

memory_buffer_alloc.c

Sep 18th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.42 KB | None | 0 0
  1. /*
  2. * Buffer-based memory allocator
  3. *
  4. * Copyright (C) 2006-2013, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  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 along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25.  
  26. #include "config.h"
  27.  
  28. #if defined(POLARSSL_MEMORY_C) && defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
  29.  
  30. #include "memory.h"
  31.  
  32. #include <linux/string.h>
  33. //#include <linux/smp.h>
  34. #include <linux/crypto.h>
  35.  
  36.  
  37.  
  38. #if defined(POLARSSL_MEMORY_DEBUG)
  39. //#include <stdio.h>
  40. #define fprintf
  41. #if defined(POLARSSL_MEMORY_BACKTRACE)
  42. #include <execinfo.h>
  43. #endif
  44. #endif
  45.  
  46. #if defined(POLARSSL_THREADING_C)
  47. #include "threading.h"
  48. #endif
  49.  
  50.  
  51.  
  52.  
  53. buffer_alloc_ctx heap[MAX_CPUS_TSX];
  54.  
  55. //unsigned char malloc_buf[MAX_CPUS_TSX][MALLOC_SIZE] __attribute__((aligned(64)));
  56.  
  57.  
  58. static int verify_header( memory_header *hdr )
  59. {
  60. if( hdr->magic1 != MAGIC1 )
  61. {
  62. return( 1 );
  63. }
  64.  
  65. if( hdr->magic2 != MAGIC2 )
  66. {
  67. return( 1 );
  68. }
  69.  
  70. if( hdr->alloc > 1 )
  71. {
  72. return( 1 );
  73. }
  74.  
  75. if( hdr->prev != NULL && hdr->prev == hdr->next )
  76. {
  77. return( 1 );
  78. }
  79.  
  80. if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
  81. {
  82. return( 1 );
  83. }
  84.  
  85. return( 0 );
  86. }
  87.  
  88. static int verify_chain(void)
  89. {
  90.  
  91. int id = smp_processor_id();
  92. #ifdef HYPER
  93. if(id == 1 || id == 3 || id == 5 || id == 7)
  94. id--;
  95. #endif
  96. memory_header *prv = heap[id].first, *cur = heap[id].first->next;
  97.  
  98. if( verify_header( heap[id].first ) != 0 )
  99. {
  100. #if defined(POLARSSL_MEMORY_DEBUG)
  101. //fprintf( stderr, "FATAL: verification of first header failed\n" );
  102. #endif
  103. return( 1 );
  104. }
  105.  
  106. if( heap[id].first->prev != NULL )
  107. {
  108. #if defined(POLARSSL_MEMORY_DEBUG)
  109. //fprintf( stderr, "FATAL: verification failed: first->prev != NULL\n" );
  110. #endif
  111. return( 1 );
  112. }
  113.  
  114. while( cur != NULL )
  115. {
  116. if( verify_header( cur ) != 0 )
  117. {
  118. #if defined(POLARSSL_MEMORY_DEBUG)
  119. //fprintf( stderr, "FATAL: verification of header failed\n" );
  120. #endif
  121. return( 1 );
  122. }
  123.  
  124. if( cur->prev != prv )
  125. {
  126. #if defined(POLARSSL_MEMORY_DEBUG)
  127. //fprintf( stderr, "FATAL: verification failed: cur->prev != prv\n" );
  128. #endif
  129. return( 1 );
  130. }
  131.  
  132. prv = cur;
  133. cur = cur->next;
  134. }
  135.  
  136. return( 0 );
  137. }
  138.  
  139. void *buffer_alloc_malloc( size_t len )
  140. {
  141. int id = smp_processor_id();
  142. #ifdef HYPER
  143. if(id == 1 || id == 3 || id == 5 || id == 7)
  144. id--;
  145. #endif
  146. memory_header *new, *cur = heap[id].first_free;
  147. unsigned char *p;
  148.  
  149.  
  150. if( heap[id].buf == NULL || heap[id].first == NULL )
  151. return( NULL );
  152.  
  153. if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
  154. {
  155. len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
  156. len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
  157. }
  158.  
  159. // Find block that fits
  160. //
  161. while( cur != NULL )
  162. {
  163. if( cur->size >= len )
  164. break;
  165.  
  166. cur = cur->next_free;
  167. }
  168.  
  169. if( cur == NULL )
  170. return( NULL );
  171. if( cur->alloc != 0 )
  172. {
  173.  
  174. //exit( 1 ); //GL/////////////
  175. return( NULL );
  176. }
  177.  
  178. #if defined(POLARSSL_MEMORY_DEBUG)
  179. heap[id].malloc_count++;
  180. #endif
  181.  
  182. // Found location, split block if > memory_header + 4 room left
  183. //
  184. if( cur->size - len < sizeof(memory_header) + POLARSSL_MEMORY_ALIGN_MULTIPLE )
  185. {
  186. cur->alloc = 1;
  187.  
  188. // Remove from free_list
  189. //
  190. if( cur->prev_free != NULL )
  191. cur->prev_free->next_free = cur->next_free;
  192. else
  193. heap[id].first_free = cur->next_free;
  194.  
  195. if( cur->next_free != NULL )
  196. cur->next_free->prev_free = cur->prev_free;
  197.  
  198. cur->prev_free = NULL;
  199. cur->next_free = NULL;
  200.  
  201. #if defined(POLARSSL_MEMORY_DEBUG)
  202. heap[id].total_used += cur->size;
  203. if( heap[id].total_used > heap[id].maximum_used)
  204. heap[id].maximum_used = heap[id].total_used;
  205. #endif
  206. #if defined(POLARSSL_MEMORY_BACKTRACE)
  207. trace_cnt = backtrace( trace_buffer, MAX_BT );
  208. cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
  209. cur->trace_count = trace_cnt;
  210. #endif
  211.  
  212. if( ( heap[id].verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ){
  213. //exit( 1 ); //GL/////////////
  214. return( NULL );
  215. }
  216.  
  217. return ( (unsigned char *) cur ) + sizeof(memory_header);
  218. }
  219.  
  220. p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
  221. new = (memory_header *) p;
  222.  
  223. new->size = cur->size - len - sizeof(memory_header);
  224. new->alloc = 0;
  225. new->prev = cur;
  226. new->next = cur->next;
  227. #if defined(POLARSSL_MEMORY_BACKTRACE)
  228. new->trace = NULL;
  229. new->trace_count = 0;
  230. #endif
  231. new->magic1 = MAGIC1;
  232. new->magic2 = MAGIC2;
  233.  
  234. if( new->next != NULL )
  235. new->next->prev = new;
  236.  
  237. // Replace cur with new in free_list
  238. //
  239. new->prev_free = cur->prev_free;
  240. new->next_free = cur->next_free;
  241. if( new->prev_free != NULL )
  242. new->prev_free->next_free = new;
  243. else
  244. heap[id].first_free = new;
  245.  
  246. if( new->next_free != NULL )
  247. new->next_free->prev_free = new;
  248.  
  249. cur->alloc = 1;
  250. cur->size = len;
  251. cur->next = new;
  252. cur->prev_free = NULL;
  253. cur->next_free = NULL;
  254.  
  255. #if defined(POLARSSL_MEMORY_DEBUG)
  256. heap[id].header_count++;
  257. heap[id].total_used += cur->size;
  258. if( heap[id].total_used > heap[id].maximum_used)
  259. heap[id].maximum_used = heap[id].total_used;
  260. #endif
  261. #if defined(POLARSSL_MEMORY_BACKTRACE)
  262. trace_cnt = backtrace( trace_buffer, MAX_BT );
  263. cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
  264. cur->trace_count = trace_cnt;
  265. #endif
  266.  
  267. if( ( heap[id].verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ){
  268. //exit( 1 ); //GL/////////////
  269. return( NULL );
  270. }
  271.  
  272. return ( (unsigned char *) cur ) + sizeof(memory_header);
  273. }
  274.  
  275. void buffer_alloc_free( void *ptr )
  276. {
  277. int id = smp_processor_id();
  278. #ifdef HYPER
  279. if(id == 1 || id == 3 || id == 5 || id == 7)
  280. id--;
  281. #endif
  282. memory_header *hdr, *old = NULL;
  283. unsigned char *p = (unsigned char *) ptr;
  284.  
  285. if( ptr == NULL || heap[id].buf == NULL || heap[id].first == NULL )
  286. return;
  287.  
  288. if( p < heap[id].buf || p > heap[id].buf + heap[id].len )
  289. {
  290. #if defined(POLARSSL_MEMORY_DEBUG)
  291. //fprintf( stderr, "FATAL: polarssl_free() outside of managed space\n" );
  292. #endif
  293.  
  294. //exit( 1 ); //GL/////////////
  295. return;
  296.  
  297. }
  298.  
  299. p -= sizeof(memory_header);
  300. hdr = (memory_header *) p;
  301.  
  302. if( verify_header( hdr ) != 0 ){
  303. //exit( 1 ); //GL/////////////
  304. return;
  305. }
  306.  
  307. if( hdr->alloc != 1 )
  308. {
  309. #if defined(POLARSSL_MEMORY_DEBUG)
  310. //fprintf( stderr, "FATAL: polarssl_free() on unallocated data\n" );
  311. #endif
  312.  
  313. //exit( 1 ); //GL/////////////
  314. return;
  315.  
  316. }
  317.  
  318. hdr->alloc = 0;
  319.  
  320. #if defined(POLARSSL_MEMORY_DEBUG)
  321. heap[id].free_count++;
  322. heap[id].total_used -= hdr->size;
  323. #endif
  324.  
  325. // Regroup with block before
  326. //
  327. if( hdr->prev != NULL && hdr->prev->alloc == 0 )
  328. {
  329. #if defined(POLARSSL_MEMORY_DEBUG)
  330. heap[id].header_count--;
  331. #endif
  332. hdr->prev->size += sizeof(memory_header) + hdr->size;
  333. hdr->prev->next = hdr->next;
  334. old = hdr;
  335. hdr = hdr->prev;
  336.  
  337. if( hdr->next != NULL )
  338. hdr->next->prev = hdr;
  339.  
  340. #if defined(POLARSSL_MEMORY_BACKTRACE)
  341. free( old->trace );
  342. #endif
  343. memset1( old, 0, sizeof(memory_header) );
  344. }
  345.  
  346. // Regroup with block after
  347. //
  348. if( hdr->next != NULL && hdr->next->alloc == 0 )
  349. {
  350. #if defined(POLARSSL_MEMORY_DEBUG)
  351. heap[id].header_count--;
  352. #endif
  353. hdr->size += sizeof(memory_header) + hdr->next->size;
  354. old = hdr->next;
  355. hdr->next = hdr->next->next;
  356.  
  357. if( hdr->prev_free != NULL || hdr->next_free != NULL )
  358. {
  359. if( hdr->prev_free != NULL )
  360. hdr->prev_free->next_free = hdr->next_free;
  361. else
  362. heap[id].first_free = hdr->next_free;
  363.  
  364. if( hdr->next_free != NULL )
  365. hdr->next_free->prev_free = hdr->prev_free;
  366. }
  367.  
  368. hdr->prev_free = old->prev_free;
  369. hdr->next_free = old->next_free;
  370.  
  371. if( hdr->prev_free != NULL )
  372. hdr->prev_free->next_free = hdr;
  373. else
  374. heap[id].first_free = hdr;
  375.  
  376. if( hdr->next_free != NULL )
  377. hdr->next_free->prev_free = hdr;
  378.  
  379. if( hdr->next != NULL )
  380. hdr->next->prev = hdr;
  381.  
  382. #if defined(POLARSSL_MEMORY_BACKTRACE)
  383. free( old->trace );
  384. #endif
  385. memset1( old, 0, sizeof(memory_header) );
  386. }
  387.  
  388. // Prepend to free_list if we have not merged
  389. // (Does not have to stay in same order as prev / next list)
  390. //
  391. if( old == NULL )
  392. {
  393. hdr->next_free = heap[id].first_free;
  394. heap[id].first_free->prev_free = hdr;
  395. heap[id].first_free = hdr;
  396. }
  397.  
  398. #if defined(POLARSSL_MEMORY_BACKTRACE)
  399. hdr->trace = NULL;
  400. hdr->trace_count = 0;
  401. #endif
  402.  
  403. if( ( heap[id].verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 ){
  404. //exit( 1 ); //GL/////////////
  405. return;
  406. }
  407. }
  408.  
  409. void memory_buffer_set_verify( int verify )
  410. {
  411. int id = smp_processor_id();
  412. #ifdef HYPER
  413. if(id == 1 || id == 3 || id == 5 || id == 7)
  414. id--;
  415. #endif
  416. heap[id].verify = verify;
  417. }
  418.  
  419. int memory_buffer_alloc_verify()
  420. {
  421. return verify_chain();
  422. }
  423.  
  424. #if defined(POLARSSL_MEMORY_DEBUG)
  425. void memory_buffer_alloc_status()
  426. {
  427. /*
  428. int id = smp_processor_id();
  429. //fprintf( stderr,
  430. "Current use: %u blocks / %u bytes, max: %u bytes, malloc / free: %u / %u\n",
  431. heap[id].header_count, heap[id].total_used, heap[id].maximum_used,
  432. heap[id].malloc_count, heap[id].free_count );
  433.  
  434. if( heap[id].first->next == NULL )
  435. //fprintf( stderr, "All memory de-allocated in stack buffer\n" );
  436. else
  437. {
  438. //fprintf( stderr, "Memory currently allocated:\n" );
  439. //debug_chain();
  440. }
  441. */
  442. }
  443. #endif /* POLARSSL_MEMORY_BUFFER_ALLOC_DEBUG */
  444.  
  445.  
  446.  
  447.  
  448.  
  449. int memory_buffer_alloc_init( )
  450. {
  451. int id = smp_processor_id();
  452. #ifdef HYPER
  453. if(id == 1 || id == 3 || id == 5 || id == 7)
  454. id--;
  455. #endif
  456. unsigned char *buf;
  457. buf = heap[id].malloc_buf;
  458. #ifdef USE_STACK
  459. memset1( heap+id, 0, heap[id].stack- (unsigned char *)&(heap[id]) );
  460. #else
  461. //memset( &heap[id], 0, sizeof(heap[id]) );
  462. memset1( heap+ id, 0, sizeof(buffer_alloc_ctx) );
  463. #endif
  464. //P_DEBUG("id: %d, buffer_alloc_ctx: %x, id: %d, buffer_alloc_ctx: %x, id: %d, buffer_alloc_ctx: %x\n",0,&(heap[0]),1,&(heap[1]),smp_processor_id(),&(heap[smp_processor_id()]));
  465.  
  466. #if defined(POLARSSL_THREADING_C)
  467. polarssl_mutex_init( heap+id.mutex );
  468. polarssl_malloc = buffer_alloc_malloc_mutexed;
  469. polarssl_free = buffer_alloc_free_mutexed;
  470. #else
  471. //polarssl_malloc = buffer_alloc_malloc;
  472. //polarssl_free = buffer_alloc_free;
  473. #endif
  474.  
  475. heap[id].buf = buf;
  476. heap[id].len = MALLOC_SIZE;
  477.  
  478. heap[id].first = (memory_header *) buf;
  479.  
  480. heap[id].first->size = MALLOC_SIZE - sizeof(memory_header);
  481. heap[id].first->magic1 = MAGIC1;
  482. heap[id].first->magic2 = MAGIC2;
  483. heap[id].first_free = heap[id].first;
  484. return( 0 );
  485. }
  486.  
  487. void memory_buffer_alloc_free()
  488. {
  489. int id = smp_processor_id();
  490. #ifdef HYPER
  491. if(id == 1 || id == 3 || id == 5 || id == 7)
  492. id--;
  493. #endif
  494. #if defined(POLARSSL_THREADING_C)
  495. polarssl_mutex_free( &heap[id].mutex );
  496. #endif
  497. memset1( heap+id, 0, sizeof(buffer_alloc_ctx) );
  498. }
  499.  
  500. #endif /* POLARSSL_MEMORY_C && POLARSSL_MEMORY_BUFFER_ALLOC_C */
  501.  
Add Comment
Please, Sign In to add comment