DSMCC  17.9.0
 All Data Structures Files Functions Typedefs
clDsmMemMgrAPI.h
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  *
5  * This file is part of a DTVKit Software Component
6  * You are permitted to copy, modify or distribute this file subject to the terms
7  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
8  *
9  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
10  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * If you or your organisation is not a member of DTVKit then you have access
14  * to this source code outside of the terms of the licence agreement
15  * and you are expected to delete this and any associated files immediately.
16  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
17  *******************************************************************************/
24 #ifndef _CLDSMMEMMGRAPI_H_
25 #define _CLDSMMEMMGRAPI_H_
26 
27 
28 /*-------------------------------- Includes ----------------------------------*/
29 
30 #include "techtype.h"
31 
32 /*-------------------------------- Defines -----------------------------------*/
33 
34 /*
35 -- ERROR CODES
36 --
37 -- To reduce unneccessary overheads in the final code, most of the memory
38 -- manager API functions do not return error codes. It is expected that the
39 -- caller (if necessary) will check parameters for legality before making the
40 -- the memory manager API call. It is sensible to do error checking
41 -- (via asserts) inside the memory manager functions during DEBUG.
42 --
43 -- Error codes are only returned by functions that can generate valid run-time
44 -- errors (ie. not due to coding errors). These errors codes are returned via
45 -- the S32BIT return value of the API funcs.
46 */
47 
48 typedef enum
49 {
50  MEM_NO_ERR,
51  MEM_ERR_CACHE_FULL,
52  MEM_ERR_ALLOC_FAILED,
53  MEM_ERR_SEQ_OPEN_LIMIT,
54  MEM_ERR_SEQ_END_OF_DATA,
55 } E_DsmMemErr;
56 
57 
58 /*
59 -- DEFAULT SIZE LIMITS
60 */
61 
62 #define MEM_BLOCK_SIZE_DFLT (256)
63 #define MEM_HEAP_SIZE_DFLT (512 * 1024)
64 #define MEM_SEQ_OPEN_DFLT (256) /* -- concurrently used MemSeqRef values */
65 
66 
67 #if defined(VGR_TRK_MEM) && !defined(USE_DSM_MEMCHK) && !defined(DBG_STORE_LINE)
68 #define DBG_STORE_LINE
69 #endif
70 
71 
72 /*-------------------------------- Types -------------------------------------*/
73 
74 /*
75 -- Generic MemHandle definition
76 --
77 -- If clDsm is supplied as a pre-built library for a particular platform it
78 -- will store a MemHandle as this type (ie. a standard pointer). This could
79 -- be cast to a different type in the memory manager if required.
80 --
81 -- If clDsm is being compiled from source then MemHandle can be (re)defined to
82 -- anything appropriate.
83 --
84 -- NOTE:
85 -- 1) Zero (Null) MUST NOT be a legal value for a MemHandle.
86 -- 2) It is rather convenient to use void*. In general, a compiler (eg 'gcc')
87 -- detects differing levels of indirection. So, does not matter is some don't.
88 */
89 typedef void* MemHandle;
90 
91 
92 /*
93 -- Generic MemSeqRef definition
94 --
95 -- If clDsm is supplied as a pre-built library for a particular platform it
96 -- will store a MemSeqRef as this type (ie. a pointer to a structure). This can
97 -- be cast to the appropriate (structure) type in the memory manager.
98 --
99 -- If clDsm is being compiled from source then MemSeqRef can be (re)defined to
100 -- anything appropriate.
101 --
102 */
103 /*typedef struct _MemSeqRef_struct *MemSeqRef; */
104 
105 typedef U8BIT *MemSeqRef;
106 
107 /*----------------------------- Exported Data --------------------------------*/
108 
109 /*------------------------------ Prototypes ----------------------------------*/
110 
111 #ifdef DSM_NO_MEM_CONTEXT
112  #define memStop( x ) memStop()
113  #ifdef DBG_STORE_LINE
114  #define memAlloc( x, i, a, l ) memAlloc( i, a, l )
115  #else
116  #define memAlloc( x, i, a, l ) memAlloc( i, a )
117  #endif
118  #define memRelease( x, i, a ) memRelease( i, a )
119  #define memSeqOpen( x, a, b, c, d, e ) memSeqOpen( a, b, c, d, e )
120  #define memNumOpen( x ) memNumOpen()
121  #define memSeqNumOpen( x ) memSeqNumOpen()
122 #else
123  #ifndef DBG_STORE_LINE
124  #define memAlloc( x, i, a, l ) memAlloc( x, i, a )
125  #endif
126 #endif
127 
128 /*
129 -- Start memory manager [SYNCHRONOUS]
130 --
131 -- Specifies minimum contiguous memory block size in bytes (mandatory).
132 -- Specifies minimum size of heap memory in bytes (mandatory).
133 -- Specifies minimum number of MemSeq areas that can be opened
134 -- simultaneously (mandatory).
135 -- Requests total size of heap memory in bytes (optional - set to memHeapSizeMin
136 -- value if not used).
137 -- Supplies reference for memSetup parameters (optional).
138 --
139 -- Returns pointer to this memory manager context (if implemented, otherewise
140 -- should be returned as Null), this will be supplied to any subsequent mem
141 -- function calls that require it.
142 --
143 -- If any problems encountered (eg. in block/heap sizes) abort start and return
144 -- error code: MEM_ERR_START_PROBLEM
145 --
146 */
147 E_DsmMemErr memStart(
148  /*I*/ U16BIT memBlockSizeMin, U32BIT memHeapSizeMin, U16BIT memSeqOpenMin,
149  void *memSetup,
150  /*O*/ void **memContext );
151 
152 
153 /*
154 -- Stop (forcibly) the memory manager [SYNCHRONOUS]
155 --
156 -- If any problems noticed (eg. memory objects still open) still complete
157 -- stop (eg. free memory) but return error code: MEM_ERR_STOP_PROBLEM
158 --
159 */
160 E_DsmMemErr memStop( void *context );
161 
162 
163 /*
164 -- Allocate an area of managed (heap) memory [SYNCHRONOUS]
165 */
166 MemHandle memAlloc( void *context, P_DsmSetup setup, U32BIT size, int line );
167 
168 /*
169 -- Release a previously allocated area of managed (heap) memory [SYNCHRONOUS]
170 */
171 U32BIT memRelease( void *context, P_DsmSetup setup, MemHandle memArea );
172 
173 
174 /*
175 -- Check if a MemHandle is valid [SYNCHRONOUS]
176 --
177 -- NB. This should be 100% reliable at determining if a MemHandle reperesents
178 -- a currently allocated (ie. not free) memory area in the memory heap.
179 --
180 */
181 BOOLEAN memValidate( void *memArea );
182 
183 
184 /*
185 -- ************************************************************************
186 -- NB. FOR DEBUG ONLY [SYNCHRONOUS]:
187 -- Return a count of number of currently open (via memOpen) memory objects.
188 -- Can be used to help detect objects left open erroneously.
189 -- ************************************************************************
190 */
191 #ifndef NDEBUG
192 U32BIT memNumOpen( void *context );
193 #endif
194 
195 
196 /*
197 -- SEQUENTIAL ('FILE TYPE') MEMORY ACCESS FUNCTIONS:
198 --
199 -- NOTES:
200 --
201 -- Only memSeqOpen requires a context to be passed (if supported). Memory
202 -- managers that do support a context must store the context value via the
203 -- MemSeqRef pointer which is passed to all other memSeq*** functions (nb. this
204 -- is because asynchronous clients using these calls will not have access to
205 -- the context value)
206 --
207 -- It is legal for a memory sequence cursor to incremented or set to the
208 -- position _immediately_ after the last byte of the sequential memory
209 -- area (ie. an error should not be generated in this case). However, it
210 -- is illegal (should generate an error) if an attempt is made to read or
211 -- write at this cursor position.
212 --
213 */
214 
215 
216 /* -- SYNCHRONOUS ONLY FUNCTIONS */
217 
218 /*
219 -- Open all or part of a memory area for sequential ('file type') access
220 -- [SYNCHRONOUS]
221 --
222 -- Offset and length specify which part of the memory area is being
223 -- accessed sequentially. They must always be set appropriately even if access
224 -- to the whole area is required (note - this should make end of data (EOF)
225 -- detection easier to implement in the memory manager).
226 --
227 -- Different parts of a single memory area can be concurrently opened if
228 -- required (note - if they overlap, then behaviour if writing asynchronously
229 -- will be unpredictable !).
230 --
231 -- The asyncAccess flag indicates whether subsequent read/write access to this
232 -- area (via the memSeq* functions) will be asynchronous or synchronous. This
233 -- holds true until the subsequent memSeqClose for this memory area (or part).
234 --
235 -- If the number of memory areas (or parts) opened concurrently for sequential
236 -- access reaches the memory manager limit (minimum value memSeqOpenMin passed
237 -- in at memStart), return error code: MEM_ERR_SEQ_OPEN_LIMIT
238 --
239 */
240 E_DsmMemErr memSeqOpen( void *context,
241  MemHandle memArea, U32BIT offset, U32BIT length, BOOLEAN asyncAccess,
242  MemSeqRef *memAreaRef );
243 
244 
245 /*
246 -- Clone/copy an open sequential memory area reference (including current cursor
247 -- position) [SYNCHRONOUS]
248 --
249 -- Allocates, opens and returns another sequential memory area (MemSeqRef value)
250 --
251 -- NB. Context can be determined from memAreaRefOrig (if needed).
252 --
253 -- If the number of memory areas (or parts) opened concurrently for sequential
254 -- access reaches the memory manager limit (minimum value memSeqOpenMin passed
255 -- in at memStart), return error code: MEM_ERR_SEQ_OPEN_LIMIT
256 --
257 */
258 E_DsmMemErr memSeqOpenClone(
259  MemSeqRef memAreaRefOrig, MemSeqRef *memAreaRefClone );
260 
261 
262 /*
263 -- Close a memory area for sequential ('file type') access [SYNCHRONOUS]
264 --
265 -- NB. Context can be determined from memAreaRef (if needed).
266 --
267 */
268 void memSeqClose( MemSeqRef memAreaRef );
269 
270 
271 /*
272 -- Get a _real_ memory pointer for the current 'cursor' position and indicate
273 -- the number of contiguous bytes of memory directly accessible from this
274 -- pointer (within this memSeq area). Advance 'cursor' by numBytesContig
275 -- [SYNCHRONOUS]
276 --
277 -- Once called, the cursor should either end up pointing to the start of the
278 -- next block (in block based memory managers) or the end of the memSeq area
279 -- (in block based or contiguous memory managers). In either case the
280 -- function can then be called again to access the next block or determine
281 -- if the end of the memSeq area has been reached. If the 'cursor' is at the
282 -- end of the memSeq area when this function is called then memPtr is returned
283 -- as Null and numContigBytes set to 0.
284 --
285 -- NB. This must be used VERY CAREFULLY since directly accessing beyond the
286 -- supplied numContigBytes is likely to cause a fatal error (especially with
287 -- block based memory managers).
288 --
289 */
290 void memSeqAccessContig(
291  MemSeqRef memAreaRef, U8BIT **memPtr, U32BIT *numContigBytes );
292 
293 void memSeqAccessCurrent(
294  MemSeqRef memAreaRef, U8BIT **memPtr, U32BIT *numContigBytes );
295 
296 /*
297 -- ************************************************************************
298 -- NB. FOR DEBUG ONLY [SYNCHRONOUS]:
299 -- Return a count of number of currently open (via memSeqOpen or
300 -- meSeqOpenClone) memory sequences
301 -- Can be used to help detect MemSeqRef areas left open erroneously.
302 -- ************************************************************************
303 */
304 #ifndef NDEBUG
305 U32BIT memSeqNumOpen( void *context );
306 #endif
307 
308 
309 
310 /* -- SYNCHRONOUS/ASYNCHRONOUS FUNCTIONS */
311 
312 /*
313 -- Check if a MemSeqRef is valid [SYNCHRONOUS/ASYNCHRONOUS]
314 */
315 void memSeqValidate( MemSeqRef memAreaRef, BOOLEAN *valid );
316 
317 
318 /*
319 -- Get size of a sequential memory area [SYNCHRONOUS/ASYNCHRONOUS]
320 --
321 -- NB. The size returned should be the exact number of bytes supplied as the
322 -- length parameter at memSeqOpen.
323 --
324 */
325 void memSeqSize( MemSeqRef memAreaRef, U32BIT *size );
326 
327 
328 /*
329 -- Read single byte from current 'cursor' position in managed memory,
330 -- increment cursor [SYNCHRONOUS/ASYNCHRONOUS]
331 --
332 -- If call attempts to read past end of the sequential memory area,
333 -- return error code: MEM_ERR_SEQ_END_OF_DATA
334 --
335 */
336 E_DsmMemErr memSeqReadByte( MemSeqRef src, U8BIT *byte );
337 
338 
339 /*
340 -- Put single byte to current 'cursor' position in managed memory,
341 -- increment cursor [SYNCHRONOUS/ASYNCHRONOUS]
342 --
343 -- If call attempts to write past end of the sequential memory area,
344 -- return error code: MEM_ERR_SEQ_END_OF_DATA
345 --
346 */
347 E_DsmMemErr memSeqWriteByte( U8BIT byte, MemSeqRef dest );
348 
349 
350 /*
351 -- Read numBytes into contiguous memory (starting at current 'cursor' position
352 -- in managed memory), increment 'cursor' position by numBytes
353 -- [SYNCHRONOUS/ASYNCHRONOUS]
354 --
355 -- If call attempts to read past end of the sequential memory area,
356 -- read as many bytes as possible, store the number actually read in
357 -- numBytesActual and return error code: MEM_ERR_SEQ_END_OF_DATA
358 --
359 */
360 E_DsmMemErr memSeqRead(
361  MemSeqRef src, U8BIT *dest, U32BIT numBytes, U32BIT *numBytesActual );
362 
363 
364 /*
365 -- Write numBytes from contiguous memory into managed memory (starting at
366 -- current 'cursor' position in managed memory), increment 'cursor' position by
367 -- numBytes [SYNCHRONOUS/ASYNCHRONOUS]
368 --
369 -- If call attempts to write past end of the sequential memory area,
370 -- write as many bytes as possible, store the number actually written in
371 -- numBytesActual and return error code: MEM_ERR_SEQ_END_OF_DATA
372 --
373 */
374 E_DsmMemErr memSeqWrite(
375  U8BIT *src, MemSeqRef dest, U32BIT numBytes, U32BIT *numBytesActual );
376 
377 
378 /*
379 -- Copy numBytes from one managed memory area into another managed memory area
380 -- (starting at current 'cursor' positions in each), increment 'cursor'
381 -- positions in each by numBytes [SYNCHRONOUS/ASYNCHRONOUS]
382 --
383 -- If call attempts to write/read past the end of either of the sequential
384 -- memory areas, copy as many bytes as possible, store the number actually
385 -- copied in numBytesActual and return error code: MEM_ERR_SEQ_END_OF_DATA
386 --
387 */
388 E_DsmMemErr memSeqCopy(
389  MemSeqRef src, MemSeqRef dest, U32BIT numBytes, U32BIT *numBytesActual );
390 
391 
392 /*
393 -- Compare sequence of bytes (numBytes long) in contiguous memory with bytes
394 -- in managed memory (starting at current 'cursor' position), do not change
395 -- 'cursor' position [SYNCHRONOUS/ASYNCHRONOUS]
396 --
397 -- If call attempts to compare bytes past the end of the sequential
398 -- memory area, return error code: MEM_ERR_SEQ_END_OF_DATA
399 --
400 */
401 E_DsmMemErr memSeqCompContig(
402  U8BIT *contig, MemSeqRef memAreaRef, U32BIT numBytes, BOOLEAN *equal );
403 
404 
405 /*
406 -- Compare sequence of bytes (numBytes long) in one managed memory area with
407 -- bytes in another managed memory area (starting at current 'cursor' positions
408 -- in each), do not change 'cursor' positions for either memory area
409 -- [SYNCHRONOUS/ASYNCHRONOUS]
410 --
411 -- If call attempts to compare bytes past the end of either of the sequential
412 -- memory areas, return error code: MEM_ERR_SEQ_END_OF_DATA
413 --
414 */
415 E_DsmMemErr memSeqCompMgd(
416  MemSeqRef memAreaRef1, MemSeqRef memAreaRef2, U32BIT numBytes,
417  BOOLEAN *equal );
418 
419 
420 /*
421 -- Set sequential access 'cursor' position relative to current position
422 -- [SYNCHRONOUS/ASYNCHRONOUS]
423 --
424 -- If call attempts to set cursor past the end of the sequential memory area,
425 -- return error code: MEM_ERR_SEQ_END_OF_DATA
426 --
427 */
428 E_DsmMemErr memSeqSetPosRel( MemSeqRef memAreaRef, S32BIT position );
429 
430 
431 /*
432 -- Set absolute sequential access 'cursor' position [SYNCHRONOUS/ASYNCHRONOUS]
433 --
434 -- If call attempts to set cursor past the end of the sequential memory area,
435 -- return error code: MEM_ERR_SEQ_END_OF_DATA
436 --
437 */
438 E_DsmMemErr memSeqSetPosAbs( MemSeqRef memAreaRef, U32BIT position );
439 
440 
441 /*
442 -- Read sequential access 'cursor' position [SYNCHRONOUS/ASYNCHRONOUS]
443 --
444 -- Value returned in 'position' is the offset from the start of the memory area
445 -- to the next byte to be read. If the current position has passed the end of
446 -- the memory area, then the position returned is equal to the memory area size.
447 -- The value returned therefore ranges from 0 to (memory area size).
448 */
449 void memSeqReadPos( MemSeqRef memAreaRef, U32BIT *position );
450 
451 
452 /*----------------------------------------------------------------------------*/
453 #endif /* _CLDSMMEMMGRAPI_H_ */
454 
System Wide Global Technical Data Type Definitions.