1/*************************************************************************/
2/* module:          Library for Memory Functions                         */
3/*                                                                       */
4/* file:            libmem.c                                             */
5/* target system:   ALL                                                  */
6/* target OS:       ALL                                                  */
7/*                                                                       */
8/* Description:                                                          */
9/* Header for the implementation of common memory handling functions     */
10/*************************************************************************/
11
12/*
13 * Copyright Notice
14 * Copyright (c) Ericsson, IBM, Lotus, Matsushita Communication
15 * Industrial Co., Ltd., Motorola, Nokia, Openwave Systems, Inc.,
16 * Palm, Inc., Psion, Starfish Software, Symbian, Ltd. (2001).
17 * All Rights Reserved.
18 * Implementation of all or part of any Specification may require
19 * licenses under third party intellectual property rights,
20 * including without limitation, patent rights (such a third party
21 * may or may not be a Supporter). The Sponsors of the Specification
22 * are not responsible and shall not be held responsible in any
23 * manner for identifying or failing to identify any or all such
24 * third party intellectual property rights.
25 *
26 * THIS DOCUMENT AND THE INFORMATION CONTAINED HEREIN ARE PROVIDED
27 * ON AN "AS IS" BASIS WITHOUT WARRANTY OF ANY KIND AND ERICSSON, IBM,
28 * LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO. LTD, MOTOROLA,
29 * NOKIA, PALM INC., PSION, STARFISH SOFTWARE AND ALL OTHER SYNCML
30 * SPONSORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
31 * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
32 * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
33 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
34 * SHALL ERICSSON, IBM, LOTUS, MATSUSHITA COMMUNICATION INDUSTRIAL CO.,
35 * LTD, MOTOROLA, NOKIA, PALM INC., PSION, STARFISH SOFTWARE OR ANY
36 * OTHER SYNCML SPONSOR BE LIABLE TO ANY PARTY FOR ANY LOSS OF
37 * PROFITS, LOSS OF BUSINESS, LOSS OF USE OF DATA, INTERRUPTION OF
38 * BUSINESS, OR FOR DIRECT, INDIRECT, SPECIAL OR EXEMPLARY, INCIDENTAL,
39 * PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND IN CONNECTION WITH
40 * THIS DOCUMENT OR THE INFORMATION CONTAINED HEREIN, EVEN IF ADVISED
41 * OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE.
42 *
43 * The above notice and this paragraph must be included on all copies
44 * of this document that are made.
45 *
46 */
47
48/*************************************************************************
49 *  Definitions
50 *************************************************************************/
51
52
53#include <smldef.h>
54#include "dmMemory.h"
55#include "libmem.h"
56#ifdef __PALM_OS__
57#include "MemoryMgr.h"
58#endif
59
60#ifdef MEMORY_PROFILING
61  // %%% luz 2002-10-02
62  #include "profiling.h"
63#endif
64
65/*************************************************************************
66 *  External Functions  for all TOOLKIT Versions
67 *************************************************************************/
68
69
70/**
71 * FUNCTION: smlLibFree
72 *
73 * Deallocates the memory of object "pObject", which has been allocated
74 * previously.
75 * If "pObject" is a NULL pointer nothing happens.
76 * If "pObject" is a pointer to memory which has not been allocated
77 * previouly, the behaviour is undefined.
78 * The contents of the deallocated memory object is destroyed.
79 */
80SML_API void smlLibFree(void *pObject)
81{
82	if (! pObject) return;
83  #ifdef __PALM_OS__
84	  MemPtrFree(pObject);
85  #else
86    DmFreeMem(pObject);
87  #endif
88}
89
90
91/**
92 * FUNCTION: smlLibRealloc
93 *
94 * Changes size of preallocated space for memory object "pObject"
95 * to the new size specified by "constSize".
96 *
97 * If the new size is larger than the old size, the old contents
98 * is not changed. Additionally space is added at the the end of
99 * "pObject". The new allocated space is not initialized
100 * to any special value.
101 * If the new size is smaller than the old size, the unused space
102 * is discarded.
103 *
104 * If "pObject" is a NULL pointer, this function behaves just like
105 * smlLibMalloc().
106 * If "pObject" does not point to a previously allocated memory area,
107 * the behavior is undefined.
108 * If "constSize" is 0, a NULL pointer is returned and the space
109 * which "pObject" points to is freed up.
110 *
111 * Returns a pointer to the first byte of the resized object.
112 * If no new memory could be allocated, a NULL Pointer is returned
113 * without changing the memory object "pObject" (Nothing happens to the content).
114 *
115 * IN/OUT           void *pObject,      memory object, which size should be changed
116 * IN:              MemSize_t constSize new size the memory object shall use
117 * RETURN:          void*               Pointer to memory object, which size has been
118 *                                      be changed
119 *                                      NULL, if not successfull or
120 *                                            if constSize==0
121 */
122SML_API void *smlLibRealloc(void *pObject, MemSize_t constSize)
123{
124#ifdef __PALM_OS__
125	VoidPtr_t	_new_object;
126	MemSize_t 	_old_size;
127
128  	// It's a malloc!
129  	if (pObject == NULL)
130    		return smlLibMalloc(constSize);
131
132  	_old_size = MemPtrSize(pObject);
133  	if (constSize <= _old_size) {
134     		// make it smaller
135     		MemPtrResize(pObject, constSize);
136     		_new_object = pObject;
137  	} else {
138     		// maker it larger (we need to allocate new memory somewhere else)
139     		_new_object = smlLibMalloc(constSize);
140     		if (_new_object != NULL) {
141        		smlLibMemmove(_new_object, pObject, _old_size);
142        		smlLibFree(pObject);
143     		}
144  	}
145
146	return _new_object;
147#else
148  return DmReallocMem(pObject, (UINT32)constSize);
149
150#endif
151}
152
153
154#ifndef __PALM_OS__
155/* If not Palm OS we use the Standard ANSI C functions */
156SML_API void *smlLibMemset(void *pObject, int value, MemSize_t count){
157	return memset(pObject, value, count);
158}
159SML_API void *smlLibMemcpy(void *pTarget, const void *pSource, MemSize_t count){
160	return memcpy(pTarget, pSource, count);
161}
162SML_API void *smlLibMemmove(void *pTarget, const void *pSource, MemSize_t count){
163	return memmove(pTarget, pSource, count);
164}
165SML_API int smlLibMemcmp(const void *pTarget, const void *pSource, MemSize_t count){
166	return memcmp(pTarget, pSource, count);
167}
168/*SML_API void *smlLibMalloc(MemSize_t size) {
169  return (void *)DmAllocMem((UINT32)size);
170
171}*/
172#endif
173
174
175
176