@@ -223,7 +223,9 @@ class zone {
223223 template <typename T>
224224 static void object_delete (void * obj);
225225
226- void * allocate_expand (size_t size);
226+ static char * get_aligned (char * ptr, size_t align);
227+
228+ char * allocate_expand (size_t size);
227229private:
228230 zone (const zone&);
229231 zone& operator =(const zone&);
@@ -233,37 +235,42 @@ inline zone::zone(size_t chunk_size) /* throw() */ :m_chunk_size(chunk_size), m_
233235{
234236}
235237
236- inline void * zone::allocate_align ( size_t size , size_t align)
238+ inline char * zone::get_aligned ( char * ptr , size_t align)
237239{
238- char * aligned =
240+ return
239241 reinterpret_cast <char *>(
240242 reinterpret_cast <size_t >(
241- (m_chunk_list.m_ptr + (align - 1 ))) / align * align);
243+ (ptr + (align - 1 ))) / align * align);
244+ }
245+
246+ inline void * zone::allocate_align (size_t size, size_t align)
247+ {
248+ char * aligned = get_aligned (m_chunk_list.m_ptr , align);
242249 size_t adjusted_size = size + (aligned - m_chunk_list.m_ptr );
243- if (m_chunk_list.m_free >= adjusted_size) {
244- m_chunk_list.m_free -= adjusted_size;
245- m_chunk_list.m_ptr += adjusted_size;
246- return aligned;
247- }
248- return reinterpret_cast <char *>(
249- reinterpret_cast <size_t >(
250- allocate_expand (size + (align - 1 ))) / align * align);
250+ if (m_chunk_list.m_free < adjusted_size) {
251+ size_t enough_size = size + align - 1 ;
252+ char * ptr = allocate_expand (enough_size);
253+ aligned = get_aligned (ptr, align);
254+ adjusted_size = size + (aligned - m_chunk_list.m_ptr );
255+ }
256+ m_chunk_list.m_free -= adjusted_size;
257+ m_chunk_list.m_ptr += adjusted_size;
258+ return aligned;
251259}
252260
253261inline void * zone::allocate_no_align (size_t size)
254262{
263+ char * ptr = m_chunk_list.m_ptr ;
255264 if (m_chunk_list.m_free < size) {
256- return allocate_expand (size);
265+ ptr = allocate_expand (size);
257266 }
258-
259- char * ptr = m_chunk_list.m_ptr ;
260267 m_chunk_list.m_free -= size;
261268 m_chunk_list.m_ptr += size;
262269
263270 return ptr;
264271}
265272
266- inline void * zone::allocate_expand (size_t size)
273+ inline char * zone::allocate_expand (size_t size)
267274{
268275 chunk_list* const cl = &m_chunk_list;
269276
@@ -285,8 +292,8 @@ inline void* zone::allocate_expand(size_t size)
285292
286293 c->m_next = cl->m_head ;
287294 cl->m_head = c;
288- cl->m_free = sz - size ;
289- cl->m_ptr = ptr + size ;
295+ cl->m_free = sz;
296+ cl->m_ptr = ptr;
290297
291298 return ptr;
292299}
0 commit comments