| 90 | */ |
| 91 | |
| 92 | static char * allocate( int32_t n ) |
| 93 | { |
| 94 | #ifdef BJAM_NEWSTR_NO_ALLOCATE |
| 95 | return (char *)BJAM_MALLOC( n ); |
| 96 | #else |
| 97 | /* See if we can grab storage from an existing block. */ |
| 98 | int32_t remaining = int32_t(storage_finish - storage_start); |
| 99 | n = ( ( n + ALLOC_ALIGNMENT - 1 ) / ALLOC_ALIGNMENT ) * ALLOC_ALIGNMENT; |
| 100 | if ( remaining >= n ) |
| 101 | { |
| 102 | char * result = storage_start; |
| 103 | storage_start += n; |
| 104 | return result; |
| 105 | } |
| 106 | else /* Must allocate a new block. */ |
| 107 | { |
| 108 | strblock * new_block; |
| 109 | int32_t nalloc = n; |
| 110 | if ( nalloc < STRING_BLOCK ) |
| 111 | nalloc = STRING_BLOCK; |
| 112 | |
| 113 | /* Allocate a new block and link into the chain. */ |
| 114 | new_block = (strblock *)BJAM_MALLOC( offsetof( strblock, data[ 0 ] ) + |
| 115 | size_t(nalloc) * sizeof( new_block->data[ 0 ] ) ); |
| 116 | if ( new_block == 0 ) |
| 117 | return 0; |
| 118 | new_block->next = strblock_chain; |
| 119 | strblock_chain = new_block; |
| 120 | |
| 121 | /* Take future allocations out of the larger remaining space. */ |
| 122 | if ( remaining < nalloc - n ) |
| 123 | { |
| 124 | storage_start = new_block->data + n; |
| 125 | storage_finish = new_block->data + nalloc; |
| 126 | } |
| 127 | return new_block->data; |
| 128 | } |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | |
| 133 | static unsigned int hash_keyval( char const * key, int32_t size ) |