Low-level OS interface. More...
#include "internal.h"
Go to the source code of this file.
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. | |
void * | _tm_os_alloc_aligned (size_t size) |
Allocate a aligned buffer aligned to tm_block_SIZE from the OS. | |
void | _tm_os_free_aligned (void *ptr, size_t size) |
Free an aligned buffer back to the OS. | |
Variables | |
size_t | tm_os_alloc_total = 0 |
Total bytes currently allocated from OS. |
Low-level OS interface.
Definition in file os.c.
void* _tm_os_alloc_aligned | ( | size_t | size | ) |
Allocate a aligned buffer aligned to tm_block_SIZE from the OS.
Return 0 if OS could not allocate a buffer.
Check alignment:
If pointer to OS block is not aligned to tm_block_SIZE,
Compute a new size to allow for alignment.
|<--- tm_BLOCK_SIZE --->|<--- tm_BLOCK_SIZE -->| +----------------------------------------------... |<--- offset --->|<--- size --->| | |<-lo->|<--- sh --->| +----------------------------------------------... ^ ^ | | ptr nb
Give the block back to the OS.
Try again with more room for realignment.
Return 0 if OS would not allocate a bigger buffer.
Realign to tm_block_SIZE.
Assert that alignment is correct.
Return aligned ptr.
Definition at line 216 of file os.c.
References _tm_os_alloc(), _tm_os_free(), tm_assert, tm_block_SIZE, tm_msg(), and tm_ptr_is_aligned_to_block.
Referenced by _tm_block_alloc().
00217 { 00218 void *ptr; 00219 00220 tm_assert(tm_ptr_is_aligned_to_block(size)); 00221 00222 ptr = _tm_os_alloc(size); 00223 00224 /*! Return 0 if OS could not allocate a buffer. */ 00225 if ( ! ptr ) 00226 return 0; 00227 00228 /** 00229 * Check alignment: 00230 * 00231 * If pointer to OS block is not aligned to tm_block_SIZE, 00232 */ 00233 if ( ! tm_ptr_is_aligned_to_block(ptr) ) { 00234 size_t offset = ((unsigned long) ptr) % tm_block_SIZE; 00235 size_t left_over = tm_block_SIZE - offset; 00236 size_t new_size = size + left_over; 00237 00238 /** 00239 * 00240 * Compute a new size to allow for alignment. 00241 * 00242 * <pre> 00243 * 00244 * |<--- tm_BLOCK_SIZE --->|<--- tm_BLOCK_SIZE -->| 00245 * +----------------------------------------------... 00246 * |<--- offset --->|<--- size --->| 00247 * | |<-lo->|<--- sh --->| 00248 * +----------------------------------------------... 00249 * ^ ^ 00250 * | | 00251 * ptr nb 00252 * 00253 * </pre> 00254 * 00255 */ 00256 tm_msg("A al %p[%lu] %lu %ld\n", (void *) ptr, (unsigned long) size, (unsigned long) tm_block_SIZE, (long) offset); 00257 00258 // THREAD-RACE on mmap()? {{{ 00259 00260 /*! Give the block back to the OS. */ 00261 _tm_os_free(ptr, size); 00262 00263 /*! Try again with more room for realignment. */ 00264 ptr = _tm_os_alloc(new_size); 00265 00266 // THREAD-RACE on mmap()? }}} 00267 00268 /*! Return 0 if OS would not allocate a bigger buffer. */ 00269 if ( ! ptr ) 00270 return 0; 00271 00272 /*! Realign to tm_block_SIZE. */ 00273 offset = ((unsigned long) ptr) % tm_block_SIZE; 00274 00275 tm_msg("A alr %p[%lu] %ld\n", (void *) ptr, (unsigned long) new_size, (long) offset); 00276 00277 if ( offset ) { 00278 left_over = tm_block_SIZE - offset; 00279 00280 ptr += left_over; 00281 new_size -= left_over; 00282 00283 /*! Assert that alignment is correct. */ 00284 tm_assert(size == new_size); 00285 tm_assert(tm_ptr_is_aligned_to_block(ptr)); 00286 tm_assert(tm_ptr_is_aligned_to_block(new_size)); 00287 } 00288 } 00289 00290 /*! Return aligned ptr. */ 00291 return ptr; 00292 }
void _tm_os_free_aligned | ( | void * | ptr, | |
size_t | size | |||
) |
Free an aligned buffer back to the OS.
Definition at line 298 of file os.c.
References _tm_os_free(), tm_assert_test, and tm_ptr_is_aligned_to_block.
Referenced by _tm_block_free().
00299 { 00300 tm_assert_test(tm_ptr_is_aligned_to_block(ptr)); 00301 tm_assert_test(tm_ptr_is_aligned_to_block(size)); 00302 00303 _tm_os_free(ptr, size); 00304 }