Allocation: Low-level

Functions

static void * _tm_os_alloc_ (long size)
 Allocate memory from the OS.
static void * _tm_os_free_ (void *ptr, long size)
 Return memory back to the OS.
static __inline void * _tm_os_alloc (long size)
 Allocate memory from OS.
static __inline void * _tm_os_free (void *ptr, long size)
 Return block to the OS.

Variables

size_t tm_os_alloc_total = 0
 Total bytes currently allocated from OS.

Function Documentation

static __inline void* _tm_os_alloc ( long  size  )  [static]

Allocate memory from OS.

  • Check soft memory limit.
  • Update stats and timers.

Definition at line 125 of file os.c.

References _tm_os_alloc_(), tm_data::n, tm, tm_assert_test, tm_B, tm_msg(), tm_os_alloc_max, tm_os_alloc_total, tm_print_stats(), tm_time_stat_begin(), tm_time_stat_end(), and tm_data::ts_os_alloc.

Referenced by _tm_os_alloc_aligned().

00126 {
00127   void *ptr;
00128 
00129 #if tm_TIME_STAT
00130   tm_time_stat_begin(&tm.ts_os_alloc);
00131 #endif
00132 
00133   /* Soft memory limit? */
00134   if ( tm_os_alloc_total + size > tm_os_alloc_max ) {
00135     ptr = 0;
00136   } else {
00137     ptr = _tm_os_alloc_(size);
00138   }
00139 
00140 #if tm_TIME_STAT
00141   tm_time_stat_end(&tm.ts_os_alloc);
00142 #endif
00143 
00144   if ( ptr == 0 || ptr == (void*) -1L ) {
00145     ptr = 0;
00146     tm_msg("A 0 %lu\n", (unsigned long) size);
00147   } else
00148   if ( size > 0 ) {
00149     tm_msg("A a %p[%lu] #%lu\n", ptr, (unsigned long) size, (unsigned long) tm.n[tm_B]);
00150 
00151     tm_os_alloc_total += size;
00152   } else if ( size < 0 ) {
00153     tm_assert_test(size > 0);
00154   }
00155 #if 0 
00156   else {
00157     tm_msg("A z\n");
00158   }
00159 #endif
00160 
00161 #if 0
00162   if ( tm.n[tm_B] > 16 ) {
00163     tm_print_stats();
00164   }
00165 #endif
00166 
00167   // fprintf(stderr, "  _tm_os_alloc(%lu) => %p\n", (unsigned long) size, (void*) ptr);
00168   
00169   return ptr;
00170 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void* _tm_os_alloc_ ( long  size  )  [static]

Allocate memory from the OS.

  • If tm_USE_MMAP is true, use mmap().
  • If tm_USE_SBRK is true, use sbrk().

Definition at line 27 of file os.c.

References tm_data::mmap_fd, tm_data::os_alloc_expected, tm_data::os_alloc_last, tm_data::os_alloc_last_size, tm, tm_abort(), tm_assert_test, and tm_warn.

Referenced by _tm_os_alloc().

00028 {
00029   void *ptr;
00030 
00031   tm_assert_test(size > 0);
00032 
00033 #if tm_USE_MMAP
00034 #ifdef MAP_ANONYMOUS
00035   tm.mmap_fd = -1;
00036 #endif
00037 
00038   ptr = mmap((void*) 0,
00039              (size_t) size,
00040              PROT_READ | PROT_WRITE,
00041 #ifdef MAP_ANONYMOUS
00042              MAP_ANONYMOUS |
00043 #endif
00044              MAP_PRIVATE,
00045              tm.mmap_fd,
00046              (off_t) 0
00047              );
00048 
00049   if ( ptr == MAP_FAILED ) {
00050     perror("tm: mmap() failed");
00051     ptr = 0;
00052     tm_abort();
00053   }
00054 #endif /* tm_USE_MMAP */
00055 
00056 #if tm_USE_SBRK
00057   ptr = sbrk(size);
00058 
00059   tm.os_alloc_last = ptr;
00060   tm.os_alloc_last_size = size;
00061 
00062   if ( tm.os_alloc_expected ) {
00063     tm_warn(ptr == tm.os_alloc_expected, "ptr = %p, expected = %p", ptr, tm.os_alloc_expected);
00064   }
00065   if ( size > 0 ) {
00066     tm.os_alloc_expected = (char *)ptr + size;
00067   }
00068 #endif /* tm_USE_SBRK */
00069 
00070 #if 0
00071   fprintf(stderr, "  _tm_os_alloc_(%lu) => %p\n", (unsigned long) size, (void*) ptr);
00072 #endif
00073 
00074   return ptr;
00075 }

Here is the call graph for this function:

Here is the caller graph for this function:

static __inline void* _tm_os_free ( void *  ptr,
long  size 
) [static]

Return block to the OS.

  • Update stats and timers.

Definition at line 179 of file os.c.

References _tm_os_free_(), _tm_page_mark_unused_range(), tm_data::n, tm, tm_B, tm_msg(), tm_os_alloc_total, tm_ptr_is_aligned_to_page, tm_time_stat_begin(), tm_time_stat_end(), and tm_data::ts_os_free.

Referenced by _tm_os_alloc_aligned(), and _tm_os_free_aligned().

00180 {
00181   void *result = 0;
00182 
00183 #if tm_TIME_STAT
00184   tm_time_stat_begin(&tm.ts_os_free);
00185 #endif
00186 
00187   result = _tm_os_free_(ptr, size);
00188 
00189 #if tm_TIME_STAT
00190   tm_time_stat_end(&tm.ts_os_free);
00191 #endif
00192 
00193   /* Mark freed pages as unused. */
00194   if ( tm_ptr_is_aligned_to_page(ptr) ) {
00195     _tm_page_mark_unused_range(ptr, size);
00196   }
00197 
00198   tm_msg("A d %p[%lu] #%lu\n", ptr, (unsigned long) size, (unsigned long) tm.n[tm_B]);
00199 
00200   tm_os_alloc_total -= size;
00201 
00202   return result;
00203 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void* _tm_os_free_ ( void *  ptr,
long  size 
) [static]

Return memory back to the OS.

  • If tm_USE_MMAP is true, use munmap().
  • If tm_USE_SBRK is true, use sbrk(- size) only if the block was the last block requested from sbrk(). Note: other callers to sbrk() may exist.

Definition at line 87 of file os.c.

References tm_data::os_alloc_expected, tm_data::os_alloc_last, tm_data::os_alloc_last_size, tm, and tm_assert_test.

Referenced by _tm_os_free().

00088 {
00089 #if 0
00090   fprintf(stderr, "  _tm_os_free_(%p, %lu)\n", (void*) ptr, (unsigned long) size);
00091 #endif
00092 
00093   tm_assert_test(ptr != 0);
00094   tm_assert_test(size > 0);
00095 
00096 #if tm_USE_MMAP
00097   munmap(ptr, size);
00098 
00099   /*! Return next allocation ptr as "unknown" (0). */
00100   return 0;
00101 #endif /* tm_USE_MMAP */
00102 
00103 #if tm_USE_SBRK
00104   if ( ptr == tm.os_alloc_last &&
00105        size == tm.os_alloc_last_size &&
00106        ptr == sbrk(0) ) {
00107     ptr = sbrk(- size);
00108 
00109     tm.os_alloc_expected -= size;
00110   }
00111 
00112   /*! Return next allocation ptr expected (non-0). */
00113   return ptr;
00114 #endif /* tm_USE_SBRK */
00115 }

Here is the caller graph for this function:


Variable Documentation

size_t tm_os_alloc_total = 0

Total bytes currently allocated from OS.

Definition at line 12 of file os.c.

Referenced by _tm_os_alloc(), and _tm_os_free().


Generated on Mon Jan 25 06:33:12 2010 for TM(tredmill) by  doxygen 1.6.1