Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-codec / rfx_pool.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol client.
3  * RemoteFX Codec Library - Memory Pool
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <freerdp/utils/memory.h>
24
25 #include "rfx_pool.h"
26
27 RFX_POOL* rfx_pool_new()
28 {
29         RFX_POOL* pool;
30
31         pool = xnew(RFX_POOL);
32
33         pool->size = 64;
34         pool->tiles = (RFX_TILE**) xzalloc(sizeof(RFX_TILE*) * pool->size);
35
36         return pool;
37 }
38
39 void rfx_pool_free(RFX_POOL* pool)
40 {
41         int i;
42         RFX_TILE* tile;
43
44         for (i = 0; i < pool->count; i++)
45         {
46                 tile = pool->tiles[i];
47
48                 if (tile != NULL)
49                 {
50                         if (tile->data != NULL)
51                                 xfree(tile->data);
52
53                         xfree(tile);
54                 }
55         }
56
57         xfree(pool->tiles);
58         xfree(pool);
59 }
60
61 void rfx_pool_put_tile(RFX_POOL* pool, RFX_TILE* tile)
62 {
63         if (pool->count >= pool->size)
64         {
65                 pool->size *= 2;
66                 pool->tiles = (RFX_TILE**) xrealloc((void*) pool->tiles, sizeof(RFX_TILE*) * pool->size);
67         }
68
69         pool->tiles[(pool->count)++] = tile;
70 }
71
72 RFX_TILE* rfx_pool_get_tile(RFX_POOL* pool)
73 {
74         RFX_TILE* tile;
75
76         if (pool->count < 1)
77         {
78                 tile = xnew(RFX_TILE);
79                 tile->data = (uint8*) xmalloc(4096 * 4); /* 64x64 * 4 */
80         }
81         else
82         {
83                 tile = pool->tiles[--(pool->count)];
84         }
85
86         return tile;
87 }
88
89 void rfx_pool_put_tiles(RFX_POOL* pool, RFX_TILE** tiles, int count)
90 {
91         int i;
92
93         for (i = 0; i < count; i++)
94         {
95                 rfx_pool_put_tile(pool, tiles[i]);
96         }
97 }
98
99 RFX_TILE** rfx_pool_get_tiles(RFX_POOL* pool, int count)
100 {
101         int i;
102         RFX_TILE** tiles;
103
104         tiles = (RFX_TILE**) xmalloc(sizeof(RFX_TILE*) * count);
105
106         for (i = 0; i < count; i++)
107         {
108                 tiles[i] = rfx_pool_get_tile(pool);
109         }
110
111         return tiles;
112 }