1 /* mblock.c -- aligned memory blocks (cache issues)
3 This file is part of the lzop file compressor.
5 Copyright (C) 1996-2010 Markus Franz Xaver Johannes Oberhumer
8 lzop and the LZO library are free software; you can redistribute them
9 and/or modify them under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzop/
32 /*************************************************************************
34 **************************************************************************/
36 static void do_init(mblock_p m, lzo_uint32 size, lzo_uint align)
38 memset(m,0,sizeof(*m));
40 m->mb_align = (align > 1) ? align : 1;
41 assert((m->mb_align & (m->mb_align - 1)) == 0);
42 m->mb_adler32 = ADLER32_INIT_VALUE;
43 m->mb_crc32 = CRC32_INIT_VALUE;
48 lzo_bool mb_init(mblock_p m, lzo_uint32 size, lzo_uint align,
49 lzo_voidp heap, lzo_uint32 heap_size)
51 do_init(m,size,align);
57 m->mb_mem_alloc = (lzo_bytep) heap;
58 m->mb_size_alloc = heap_size;
59 assert(m->mb_size_alloc >= m->mb_size + m->mb_align - 1);
61 m->mb_mem = LZO_PTR_ALIGN_UP(m->mb_mem_alloc,m->mb_align);
62 assert(m->mb_mem >= m->mb_mem_alloc);
63 assert(m->mb_mem + m->mb_size <= m->mb_mem_alloc + m->mb_size_alloc);
65 printf("m_init: %p %p %8ld %8ld %8ld\n", m->mb_mem_alloc, m->mb_mem,
66 (long) m->mb_size_alloc, (long) m->mb_size, (long) m->mb_align);
73 lzo_bool mb_alloc(mblock_p m, lzo_uint32 size, lzo_uint align)
75 do_init(m,size,align);
79 m->mb_size_alloc = m->mb_size + m->mb_align - 1;
80 m->mb_mem_alloc = (lzo_bytep) acc_halloc(m->mb_size_alloc);
81 if (m->mb_mem_alloc == NULL)
83 acc_hmemset(m->mb_mem_alloc, 0, m->mb_size_alloc);
85 m->mb_mem = LZO_PTR_ALIGN_UP(m->mb_mem_alloc,m->mb_align);
86 assert(m->mb_mem >= m->mb_mem_alloc);
87 assert(m->mb_mem + m->mb_size <= m->mb_mem_alloc + m->mb_size_alloc);
89 printf("m_alloc: %p %p %8ld %8ld %8ld\n", m->mb_mem_alloc, m->mb_mem,
90 (long) m->mb_size_alloc, (long) m->mb_size, (long) m->mb_align);
96 void mb_free(mblock_p m)
98 acc_hfree(m->mb_mem_alloc);
99 memset(m,0,sizeof(*m));