File: memhandling.c

package info (click to toggle)
libproplist 0.7.1-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 684 kB
  • ctags: 452
  • sloc: ansic: 3,684; sh: 1,856; lex: 181; yacc: 118; makefile: 106; tcl: 40
file content (205 lines) | stat: -rw-r--r-- 4,730 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* memhandling.c: This is -*- c -*-

 This file contains destroying / copying / retaining functions.

 */

#include <stdlib.h>

#include "proplist.h"
#include "proplistP.h"
#include "util.h"

#ifdef DEBUG
unsigned long num_objects;
#endif


void PLRelease(proplist_t pl)
{
  plptr_t internal;
  int i;

  internal = (plptr_t)pl;
  internal->retain_count--;
#ifdef DEBUG
  num_objects--;
  printf("Releasing object %s\n", PLGetDescription(pl));
  printf("\t(count: %ld, num_objects: %ld)\n", internal->retain_count,
	 num_objects);
#endif

  switch(internal->type)
    {
    case PLSTRING:
      if(!internal->retain_count)
	{
	  if(internal->t.str.string)
	    MyFree(__FILE__, __LINE__, internal->t.str.string);
	  if(internal->filename)
	    PLRelease(internal->filename);
	  MyFree(__FILE__, __LINE__, pl);
	}
      break;
    case PLDATA:
      if(!internal->retain_count)
	{
	  if(internal->t.data.data)
	    MyFree(__FILE__, __LINE__, internal->t.data.data);
	  if(internal->filename)
	    PLRelease(internal->filename);
	  MyFree(__FILE__, __LINE__, pl);
	}
      break;
    case PLARRAY:
      for(i=0;i<internal->t.array.number;i++)
	PLRelease(internal->t.array.elements[i]);
      if(!internal->retain_count)
	{
	  if(internal->t.array.elements)
	    MyFree(__FILE__, __LINE__, internal->t.array.elements);
	  if(internal->filename)
	    PLRelease(internal->filename);
	  MyFree(__FILE__, __LINE__, pl);
	}
      break;
    case PLDICTIONARY:
      for(i=0;i<internal->t.dict.number;i++)
	{
	  PLRelease(internal->t.dict.keys[i]);
	  PLRelease(internal->t.dict.values[i]);
	}
      if(!internal->retain_count)
	{
	  if(internal->t.dict.keys)
	    MyFree(__FILE__, __LINE__, internal->t.dict.keys);
	  if(internal->t.dict.values)
	    MyFree(__FILE__, __LINE__, internal->t.dict.values);
	  if(internal->filename)
	    PLRelease(internal->filename);
	  MyFree(__FILE__, __LINE__, pl);
	}
      break;
    default:
      break;
    }
}

proplist_t PLShallowCopy(proplist_t pl)
{
  plptr_t internal;
  proplist_t ret;
  int i;
  proplist_t allkeys, key, value;
  
  internal = (plptr_t)pl;

  switch(internal->type)
    {
    case PLSTRING:
    case PLDATA:
      return PLDeepCopy(pl);
    case PLARRAY:
      ret = PLMakeArrayFromElements(NULL);
      for(i=0; i<PLGetNumberOfElements(pl); i++)
	PLAppendArrayElement(ret, PLGetArrayElement(pl, i));
      return ret;
    case PLDICTIONARY:
      ret = PLMakeDictionaryFromEntries(NULL, NULL);
      allkeys = PLGetAllDictionaryKeys(pl);
      for(i=0; i<PLGetNumberOfElements(allkeys); i++)
	{
	  key = PLGetArrayElement(allkeys, i);
	  value = PLGetDictionaryEntry(pl, key);
	  PLInsertDictionaryEntry(ret, key, value);
	}
      PLRelease(allkeys);
    default:
      return NULL;
    }
}

proplist_t PLDeepCopy(proplist_t pl)
{
  plptr_t internal;
  proplist_t ret;
  int i;

  internal = (plptr_t)pl;

  switch(internal->type)
    {
    case PLSTRING:
      ret = PLMakeString(internal->t.str.string);
      if(internal->filename)
	PLSetFilename(ret, internal->filename);
      return ret;
    case PLDATA:
      ret = PLMakeData(internal->t.data.data,
		       internal->t.data.length);
      if(internal->filename)
	PLSetFilename(ret, internal->filename);
      return ret;
    case PLARRAY:
      ret = PLMakeArrayFromElements(NULL);
      for(i=0;i<internal->t.array.number;i++)
	{
	  proplist_t el = PLDeepCopy(internal->t.array.elements[i]);
	  PLAppendArrayElement(ret, el);
	  PLRelease(el);
	}
      if(internal->filename)
	PLSetFilename(ret, internal->filename);
      return ret;
    case PLDICTIONARY:
      ret = PLMakeDictionaryFromEntries(NULL, NULL);
      for(i=0;i<internal->t.dict.number;i++)
	{
	  proplist_t key, value;
	  key = PLDeepCopy(internal->t.dict.keys[i]);
	  value = PLDeepCopy(internal->t.dict.values[i]);
	  PLInsertDictionaryEntry(ret, key, value);
	  PLRelease(key); PLRelease(value);
	}
      if(internal->filename)
	PLSetFilename(ret, internal->filename);
      return ret;
    default:
      return NULL;
    }
}


proplist_t PLRetain(proplist_t pl)
{
  plptr_t internal = (plptr_t)pl;
  int i;

  internal->retain_count++;

#ifdef DEBUG
  num_objects++;
  printf("Retaining object %s\n", PLGetDescription(pl));
  printf("\t(count: %ld, num_objects: %ld)\n", internal->retain_count, num_objects);
#endif
  
  switch(internal->type)
    {
    case PLSTRING:
    case PLDATA:
      return pl;
    case PLARRAY:
      for(i=0; i<internal->t.array.number; i++)
	PLRetain(internal->t.array.elements[i]);
      return pl;
    case PLDICTIONARY:
      for(i=0; i<internal->t.dict.number; i++)
	{
	  PLRetain(internal->t.dict.keys[i]);
	  PLRetain(internal->t.dict.values[i]);
	}
      return pl;
    default:
      return NULL;
    }
}