root/trunk/thune/array.c

Revision 558, 4.9 kB (checked in by krobillard, 6 weeks ago)

Thune

  • Pop now pushes last element onto stack.
  • Added ur_strCatInt() & ur_strCatInt64().
  • Array now uses realloc().

Thune-GL

  • gx_startup() now takes userClasses and userLayoutRules.
  • Moved 'layout to gx.t and test style to style/oxif.t.
  • GUI layout now done with 'parse.
  • Eliminated gui_parseLayout() and GWidgetClass::parse.
  • Renamed display.events to handle-events. Removed wait-events.
  • Window widget! now has a title and a close/resize event handler.
Line 
1/*============================================================================
2    Urlan Interpreter
3    Copyright (C) 2005-2006  Karl Robillard
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18===========================================================================*/
19
20
21#include "os.h"
22#include "urlan.h"
23
24
25void ur_arrayInit( UArray* arr, int elemSize, int elemCount )
26{
27    long size = elemSize * elemCount;
28    if( size )
29    {
30        arr->ptr.v = memAlloc( size );
31        if( arr->ptr.v )
32            arr->avail = elemCount;
33        else
34            arr->avail = 0;
35    }
36    else
37    {
38        arr->ptr.v = 0;
39        arr->avail = 0;
40    }
41    arr->used  = 0;
42}
43
44
45/**
46  Frees the memory pointed to by arr->ptr.
47*/
48void ur_arrayFree( UArray* arr )
49{
50    assert( arr );
51
52    if( arr->ptr.v )
53    {
54        memFree( arr->ptr.v );
55        arr->ptr.v = 0;
56        arr->avail = 0;
57        arr->used  = 0;
58    }
59}
60
61
62/**
63  Allocates enough memory to hold elemCount elements.
64  arr->used is not changed.
65*/
66void ur_arrayReserve( UArray* arr, int elemSize, int elemCount )
67{
68    assert( arr );
69    assert( arr->avail > -1 );
70
71    if( elemCount > arr->avail )
72    {
73        int count;
74        void* buf;
75
76        /* Double the buffer size (unless that is not big enough). */
77        count = arr->avail * 2;
78        if( count < elemCount )
79        {
80            /* Using a minimum size of 4 rather than 8 results in ~10% less
81               wasted memory.  Using 2 results in ~13% less.
82               Waste seems to run 100%-150% of used memory.
83             */
84            if( elemSize >= 16 )
85                count = (elemCount < 4) ? 4 : elemCount;
86            else
87                count = (elemCount < 8) ? 8 : elemCount;
88        }
89
90#if 1
91        buf = memRealloc( arr->ptr.v, elemSize * count );
92        assert( buf );
93        //printf( "realloc %d\n", buf == arr->ptr.v );
94        arr->ptr.v = buf;
95        arr->avail = count;
96#else
97        buf = memAlloc( elemSize * count );
98        assert( buf );
99        if( buf )
100        {
101            if( arr->ptr.v )
102            {
103                if( arr->used )
104                    memCpy( buf, arr->ptr.v, elemSize * arr->used );
105                memFree( arr->ptr.v );
106            }
107            arr->ptr.v = buf;
108            arr->avail = count;
109        }
110#endif
111    }
112}
113
114
115/**
116  Remove count elements from the array starting at index.
117*/
118void ur_arrayErase( UArray* arr, int elemSize, int index, int count )
119{
120    assert( arr );
121    assert( index < arr->used );
122
123    if( (index + count) >= arr->used )
124    {
125        arr->used = index;
126        return;
127    }
128
129    if( elemSize == 1 )
130    {
131        char* buf;
132        char* src;
133        char* end;
134
135        buf = arr->ptr.c + index;
136        src = buf + count;
137        end = arr->ptr.c + arr->used;
138
139        while( src != end )
140            *buf++ = *src++;
141    }
142    else if( elemSize == 16 )
143    {
144#if __WORDSIZE == 64
145        uint64_t* buf;
146        uint64_t* src;
147        uint64_t* end;
148
149        buf = ((uint64_t*) arr->ptr.i) + (index * 2);
150        src = buf + (count * 2);
151        end = ((uint64_t*) arr->ptr.i) + (arr->used * 2);
152
153        while( src != end )
154        {
155            *buf++ = *src++;
156            *buf++ = *src++;
157        }
158#else
159        uint32_t* buf;
160        uint32_t* src;
161        uint32_t* end;
162
163        buf = ((uint32_t*) arr->ptr.i) + (index * 4);
164        src = buf + (count * 4);
165        end = ((uint32_t*) arr->ptr.i) + (arr->used * 4);
166
167        while( src != end )
168        {
169            *buf++ = *src++;
170            *buf++ = *src++;
171            *buf++ = *src++;
172            *buf++ = *src++;
173        }
174#endif
175    }
176    else
177    {
178        char* buf;
179        buf = arr->ptr.c + (elemSize * index);
180        memMove( buf, buf + (elemSize * count),
181                 elemSize * (arr->used - index - count) );
182    }
183
184    arr->used -= count;
185}
186
187
188/**
189  Create space in the array for count elements starting at index.
190  The memory in the new space is uninitialized.
191*/
192void ur_arrayExpand( UArray* arr, int elemSize, int index, int count )
193{
194    ur_arrayReserve( arr, elemSize, arr->used + count );
195
196    if( index < arr->used )
197    {
198        char* buf = arr->ptr.c + (elemSize * index);
199        memMove( buf + (elemSize * count), buf,
200                 elemSize * (arr->used - index) );
201    }
202
203    arr->used += count;
204}
205
206
207/*EOF*/
Note: See TracBrowser for help on using the browser.