MHEG5  18.9.0
MHEG5 Documentation
fpa1_BlockAlloc.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2004 Ocean Blue Software Ltd
4  * Copyright © 2000 Koninklijke Philips Electronics N.V
5  *
6  * This file is part of a DTVKit Software Component
7  * You are permitted to copy, modify or distribute this file subject to the terms
8  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
9  *
10  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
11  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
12  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * If you or your organisation is not a member of DTVKit then you have access
15  * to this source code outside of the terms of the licence agreement
16  * and you are expected to delete this and any associated files immediately.
17  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
18  *******************************************************************************/
27 /*---includes for this file--------------------------------------------------*/
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 #include "vpa1_sys.h"
32 #include "vpa1_tgs.h"
33 #include "fpa1_syn.h"
34 #include "pa1_mem.h"
35 #include "mh5debug.h"
36 #include "fpa1_BlockAlloc.h"
37 
38 /*---constant definitions for this file--------------------------------------*/
39 /* Number of syntaxListItems in each memory block */
40 #define ASN_MEM_BLOCK 0x100
41 
42 /*---local typedef structs for this file-------------------------------------*/
44 {
46  unsigned short count;
47  unsigned short check;
49 };
51 
52 
53 /*---local (static) variable declarations for this file----------------------*/
54 static fpa1_syntaxBlock *current_blk = NULL;
55 static fpa1_syntaxBlock *first_block = NULL;
56 
57 /*---local function definitions----------------------------------------------*/
58 
59 /*---global function definitions---------------------------------------------*/
60 
69 {
70  fpa1_syntaxList *newItem;
71 
72  if (current_blk->count == ASN_MEM_BLOCK)
73  {
74  if (current_blk->next == NULL)
75  {
76  fpa1_syntaxBlock *new_block;
77  new_block = (fpa1_syntaxBlock *) pa1_malloc( sizeof(fpa1_syntaxBlock));
78  if (new_block != NULL)
79  {
80  memset( new_block, 0, sizeof(fpa1_syntaxBlock));
81  current_blk->next = new_block;
82  TRACE(TMEMORY, ("new_blk=%p", new_block))
83  }
84  else
85  {
86  return NULL;
87  }
88  }
89  current_blk = current_blk->next;
90  }
91 
92  /* get next structure from the list */
93  newItem = &(current_blk->items[current_blk->count]);
94  current_blk->count++;
95 
96  newItem->data.intData = 0;
97  newItem->dataType = 0;
98 
99  return newItem;
100 }
101 
109 {
110  fpa1_syntaxBlock *temp_block;
111  int i;
112  temp_block = first_block;
113  /* free strings on previous blocks */
114  while (temp_block != current_blk)
115  {
116  TRACE(TMEMORY, ("temp_blk=%p, cnt=%d, chk=%d", temp_block, temp_block->count, temp_block->check))
117  for (i = 0; i != ASN_MEM_BLOCK; i++)
118  {
119  if (temp_block->items[i].data.stringData.data != NULL)
120  {
121  STR_DataFree( temp_block->items[i].data.stringData.data,
122  temp_block->items[i].data.stringData.len );
123  temp_block->items[i].data.stringData.data = NULL;
124  temp_block->items[i].data.stringData.len = 0;
125  }
126  }
127  temp_block->count = 0;
128  temp_block->check = 0;
129  temp_block = temp_block->next;
130  }
131  /* free strings on current block */
132  for (i = 0; i != temp_block->count; i++)
133  {
134  if (temp_block->items[i].data.stringData.data != NULL)
135  {
136  STR_DataFree( temp_block->items[i].data.stringData.data,
137  temp_block->items[i].data.stringData.len );
138  temp_block->items[i].data.stringData.data = NULL;
139  temp_block->items[i].data.stringData.len = 0;
140  }
141  }
142  TRACE(TMEMORY, ("temp_blk=%p, cnt=%d, chk=%d", temp_block, temp_block->count, temp_block->check))
143  temp_block->count = 0;
144  temp_block->check = 0;
145  current_blk = temp_block;
146  temp_block = temp_block->next;
147  while (temp_block != NULL)
148  {
149  if (temp_block->check != 8)
150  {
151  TRACE(TMEMORY, ("temp_blk=%p chk=%d", temp_block, temp_block->check))
152  temp_block->check++;
153  current_blk = temp_block;
154  temp_block = temp_block->next;
155  }
156  else
157  {
158  TRACE(TMEMORY, ("free blk=%p", temp_block))
159  if (current_blk->next == temp_block)
160  {
161  current_blk->next = NULL;
162  }
163  current_blk = temp_block->next;
164  pa1_free(temp_block);
165  temp_block = current_blk;
166  }
167  }
168  current_blk = first_block;
169  return 0;
170 }
171 
180 {
181  fpa1_syntaxList *nextComp = firstComp;
182 
183  if (nextComp != NULL)
184  {
185  /* are there any children */
186  if (firstComp->children != NULL)
187  {
188  fpa1_delList(firstComp->children);
189  firstComp->children = NULL;
190  }
191 
192  /* get next item in list - for return */
193  nextComp = nextComp->next;
194 
195  //TRACE(TMEMORY,("Free %p posn %d tag %d", firstComp, firstComp->tagPosition, firstComp->tag ))
196 
197  /* clean up structure pointers */
198  firstComp->next = NULL;
199  }
200  return nextComp;
201 }
202 
211 {
212  while (firstComp != NULL)
213  {
214  firstComp = fpa1_synFreeFirstItem( firstComp );
215  }
216 
217  return 0;
218 }
219 
220 int ASN1_MemInit(void)
221 {
222  assert( first_block == NULL );
223  first_block = (fpa1_syntaxBlock *) pa1_malloc( sizeof(fpa1_syntaxBlock));
224  if (first_block != NULL)
225  {
226  memset( first_block, 0, sizeof(fpa1_syntaxBlock));
227  }
228  current_blk = first_block;
229  TRACE(TMEMORY, ("first_blk=%p", first_block))
230  return (first_block == NULL) ? 0 : 1;
231 }
232 
233 void ASN1_MemExit(void)
234 {
235  fpa1_syntaxBlock *temp_block;
236  temp_block = first_block;
237  while (temp_block != NULL)
238  {
239  current_blk = temp_block->next;
240  pa1_free(temp_block);
241  temp_block = current_blk;
242  }
243  first_block = NULL;
244 }
245 
fpa1_syntaxList * fpa1_getTagItem(void)
int ASN1_MemInit(void)
int fpa1_delList(fpa1_syntaxList *firstComp)
struct fpa1_synBlock * next
fpa1_syntaxList * fpa1_delFirstItem(fpa1_syntaxList *firstComp)
fpa1_syntaxList * fpa1_synFreeFirstItem(fpa1_syntaxList *)
Definition: fpa1_syn.c:198
MHEG5String stringData
Definition: fpa1_syn.h:46
Contains functions/structure used to do MHEG-5 ASN.1 syntax parsing.
Typedefs, macros used by all of parser. These may be duplicated elsewhere.
struct fpa1_syntaxItem * children
Definition: fpa1_syn.h:53
syntaxItemData data
Definition: fpa1_syn.h:57
void ASN1_MemExit(void)
Contains macros for MHEG-5 ASN.1 tags and structures.
int fpa1_freeUnusedData(void)
void STR_DataFree(unsigned char *data, unsigned int size)
Definition: glue_memory.c:668
syntax item block manager Contains functions to create a mini memory manager for allocating syntax li...
#define pa1_malloc(size)
Definition: pa1_mem.h:33
MHEG5Byte * data
Definition: mh5base.h:85
unsigned short count
#define pa1_free(what)
Definition: pa1_mem.h:34
fpa1_syntaxList items[ASN_MEM_BLOCK]
Mheg5 logging and debug printing.
Contains memory management functions.
MHEG5Int len
Definition: mh5base.h:84
#define ASN_MEM_BLOCK
struct fpa1_syntaxItem * next
Definition: fpa1_syn.h:52
unsigned short check
MHEG5Int intData
Definition: fpa1_syn.h:45
#define TRACE(t, x)
Definition: glue_debug.h:118