1 /* GStreamer
2 * Copyright (C) <2009> Collabora Ltd
3 * @author: Olivier Crete <olivier.crete@collabora.co.uk
4 * Copyright (C) <2009> Nokia Inc
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "shmalloc.h"
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35
36 /* This is the allocated space to hold multiple blocks */
37 struct _ShmAllocSpace
38 {
39 /* The total size of this space */
40 size_t size;
41
42 /* chained list of the blocks contained in this space */
43 ShmAllocBlock *blocks;
44 };
45
46 /* A single block of data */
47 struct _ShmAllocBlock
48 {
49 int use_count;
50
51 /* Pointer back to the AllocSpace where this block is */
52 ShmAllocSpace *space;
53
54 /* The offset of this block in the alloc space */
55 unsigned long offset;
56 /* The size of the block */
57 unsigned long size;
58
59 /* Pointer to the next block in the chain */
60 ShmAllocBlock *next;
61 };
62
63
64 ShmAllocSpace *
shm_alloc_space_new(size_t size)65 shm_alloc_space_new (size_t size)
66 {
67 ShmAllocSpace *self = spalloc_new (ShmAllocSpace);
68
69 memset (self, 0, sizeof (ShmAllocSpace));
70
71 self->size = size;
72
73 return self;
74 }
75
76 void
shm_alloc_space_free(ShmAllocSpace * self)77 shm_alloc_space_free (ShmAllocSpace * self)
78 {
79 assert (self && self->blocks == NULL);
80 spalloc_free (ShmAllocSpace, self);
81 }
82
83
84 ShmAllocBlock *
shm_alloc_space_alloc_block(ShmAllocSpace * self,unsigned long size)85 shm_alloc_space_alloc_block (ShmAllocSpace * self, unsigned long size)
86 {
87 ShmAllocBlock *block;
88 ShmAllocBlock *item = NULL;
89 ShmAllocBlock *prev_item = NULL;
90 unsigned long prev_end_offset = 0;
91
92
93 for (item = self->blocks; item; item = item->next) {
94 unsigned long max_size = 0;
95
96 max_size = item->offset - prev_end_offset;
97
98 if (max_size >= size)
99 break;
100
101 prev_end_offset = item->offset + item->size;
102 prev_item = item;
103 }
104
105 /* Return NULL if there is no big enough space, otherwise, there is space
106 * at the end */
107 assert (prev_end_offset <= self->size);
108 if (!item && self->size - prev_end_offset < size)
109 return NULL;
110
111 block = spalloc_new (ShmAllocBlock);
112 memset (block, 0, sizeof (ShmAllocBlock));
113 block->offset = prev_end_offset;
114 block->size = size;
115 block->use_count = 1;
116 block->space = self;
117
118 if (prev_item)
119 prev_item->next = block;
120 else
121 self->blocks = block;
122
123 block->next = item;
124
125 return block;
126 }
127
128 unsigned long
shm_alloc_space_alloc_block_get_offset(ShmAllocBlock * block)129 shm_alloc_space_alloc_block_get_offset (ShmAllocBlock * block)
130 {
131 return block->offset;
132 }
133
134 static void
shm_alloc_space_free_block(ShmAllocBlock * block)135 shm_alloc_space_free_block (ShmAllocBlock * block)
136 {
137 ShmAllocBlock *item = NULL;
138 ShmAllocBlock *prev_item = NULL;
139 ShmAllocSpace *self = block->space;
140
141 for (item = self->blocks; item; item = item->next) {
142 if (item == block) {
143 if (prev_item)
144 prev_item->next = item->next;
145 else
146 self->blocks = item->next;
147 break;
148 }
149 prev_item = item;
150 }
151
152 spalloc_free (ShmAllocBlock, block);
153 }
154
155 ShmAllocBlock *
shm_alloc_space_block_get(ShmAllocSpace * self,unsigned long offset)156 shm_alloc_space_block_get (ShmAllocSpace * self, unsigned long offset)
157 {
158 ShmAllocBlock *block = NULL;
159
160 for (block = self->blocks; block; block = block->next) {
161 if (block->offset <= offset && (block->offset + block->size) > offset)
162 return block;
163 }
164
165 return NULL;
166 }
167
168
169 void
shm_alloc_space_block_inc(ShmAllocBlock * block)170 shm_alloc_space_block_inc (ShmAllocBlock * block)
171 {
172 block->use_count++;
173 }
174
175 void
shm_alloc_space_block_dec(ShmAllocBlock * block)176 shm_alloc_space_block_dec (ShmAllocBlock * block)
177 {
178 block->use_count--;
179
180 if (block->use_count <= 0)
181 shm_alloc_space_free_block (block);
182 }
183