09566cd86e60db2ed4889f514fa6dfe98e297e26
[platform/upstream/lzop.git] / src / mblock.c
1 /* mblock.c -- aligned memory blocks (cache issues)
2
3    This file is part of the lzop file compressor.
4
5    Copyright (C) 1996-2010 Markus Franz Xaver Johannes Oberhumer
6    All Rights Reserved.
7
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.
12
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.
17
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.
22
23    Markus F.X.J. Oberhumer
24    <markus@oberhumer.com>
25    http://www.oberhumer.com/opensource/lzop/
26  */
27
28
29 #include "conf.h"
30
31
32 /*************************************************************************
33 //
34 **************************************************************************/
35
36 static void do_init(mblock_p m, lzo_uint32 size, lzo_uint align)
37 {
38     memset(m,0,sizeof(*m));
39     m->mb_size = size;
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;
44 }
45
46
47 #if 0
48 lzo_bool mb_init(mblock_p m, lzo_uint32 size, lzo_uint align,
49                  lzo_voidp heap, lzo_uint32 heap_size)
50 {
51     do_init(m,size,align);
52     if (m->mb_size == 0)
53         return 1;
54
55     if (heap == 0)
56         return 0;
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);
60
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);
64 #if 0
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);
67 #endif
68     return 1;
69 }
70 #endif
71
72
73 lzo_bool mb_alloc(mblock_p m, lzo_uint32 size, lzo_uint align)
74 {
75     do_init(m,size,align);
76     if (m->mb_size == 0)
77         return 1;
78
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)
82         return 0;
83     acc_hmemset(m->mb_mem_alloc, 0, m->mb_size_alloc);
84
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);
88 #if 0
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);
91 #endif
92     return 1;
93 }
94
95
96 void mb_free(mblock_p m)
97 {
98     acc_hfree(m->mb_mem_alloc);
99     memset(m,0,sizeof(*m));
100 }
101
102
103 /*
104 vi:ts=4:et
105 */
106