OpenNI 1.5.7
XnGeneralBuffer.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2012 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * Licensed under the Apache License, Version 2.0 (the "License"); *
9 * you may not use this file except in compliance with the License. *
10 * You may obtain a copy of the License at *
11 * *
12 * http://www.apache.org/licenses/LICENSE-2.0 *
13 * *
14 * Unless required by applicable law or agreed to in writing, software *
15 * distributed under the License is distributed on an "AS IS" BASIS, *
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17 * See the License for the specific language governing permissions and *
18 * limitations under the License. *
19 * *
20 *****************************************************************************/
21 #ifndef __XN_GENERAL_BUFFER_H__
22 #define __XN_GENERAL_BUFFER_H__
23 
24 //---------------------------------------------------------------------------
25 // Includes
26 //---------------------------------------------------------------------------
27 #include "XnPlatform.h"
28 #include "XnOS.h"
29 #include "XnStatusCodes.h"
30 
31 //---------------------------------------------------------------------------
32 // Types
33 //---------------------------------------------------------------------------
34 
35 /* Describes a general buffer. */
36 typedef struct XnGeneralBuffer
37 {
38  /* A pointer to the actual data. */
39  void* pData;
40  /* The size of the data in bytes. */
41  XnUInt32 nDataSize;
43 
44 //---------------------------------------------------------------------------
45 // Exported Functions
46 //---------------------------------------------------------------------------
47 
51 inline XnGeneralBuffer XnGeneralBufferPack(void* pData, XnUInt32 nDataSize)
52 {
53  XnGeneralBuffer result;
54  result.pData = pData;
55  result.nDataSize = nDataSize;
56  return result;
57 }
58 
63 {
64  XN_VALIDATE_INPUT_PTR(pDest);
66 
67  if (pSrc->nDataSize > pDest->nDataSize)
68  return XN_STATUS_OUTPUT_BUFFER_OVERFLOW;
69 
70  xnOSMemCopy(pDest->pData, pSrc->pData, pSrc->nDataSize);
71  pDest->nDataSize = pSrc->nDataSize;
72  return XN_STATUS_OK;
73 }
74 
75 inline XnStatus XnGeneralBufferAlloc(XnGeneralBuffer* pDest, XnUInt32 nSize)
76 {
77  XN_VALIDATE_INPUT_PTR(pDest);
78 
79  void* pData;
80  pData = xnOSMalloc(nSize);
81  XN_VALIDATE_ALLOC_PTR(pData);
82 
83  pDest->pData = pData;
84  pDest->nDataSize = nSize;
85  return XN_STATUS_OK;
86 }
87 
88 inline XnStatus XnGeneralBufferRealloc(XnGeneralBuffer* pDest, XnUInt32 nSize)
89 {
90  XN_VALIDATE_INPUT_PTR(pDest);
91 
92  void* pData;
93  pData = xnOSRealloc(pDest, nSize);
94  XN_VALIDATE_ALLOC_PTR(pData);
95 
96  pDest->pData = pData;
97  pDest->nDataSize = nSize;
98  return XN_STATUS_OK;
99 }
100 
102 {
103  XN_FREE_AND_NULL(pDest->pData);
104  pDest->nDataSize = 0;
105 }
106 
107 //---------------------------------------------------------------------------
108 // Helper Macros
109 //---------------------------------------------------------------------------
110 #define XN_PACK_GENERAL_BUFFER(x) XnGeneralBufferPack(&x, sizeof(x))
111 
112 #define XN_VALIDATE_GENERAL_BUFFER_TYPE(gb, t) \
113  if ((gb).nDataSize != sizeof(t)) \
114  { \
115  return XN_STATUS_INVALID_BUFFER_SIZE; \
116  }
117 
118 #endif //__XN_GENERAL_BUFFER_H__
XN_C_API void *XN_C_DECL xnOSRealloc(void *pMemory, const XnSizeT nAllocSize)
#define XN_VALIDATE_ALLOC_PTR(x)
Definition: XnOS.h:127
XnStatus XnGeneralBufferAlloc(XnGeneralBuffer *pDest, XnUInt32 nSize)
Definition: XnGeneralBuffer.h:75
#define XN_STATUS_OK
Definition: XnStatus.h:36
void * pData
Definition: XnGeneralBuffer.h:39
XnUInt32 XnStatus
Definition: XnStatus.h:33
Definition: XnGeneralBuffer.h:36
XN_C_API void XN_C_DECL xnOSMemCopy(void *pDest, const void *pSource, XnSizeT nCount)
XN_C_API void *XN_C_DECL xnOSMalloc(const XnSizeT nAllocSize)
#define XN_VALIDATE_INPUT_PTR(x)
Definition: XnOS.h:122
XnGeneralBuffer XnGeneralBufferPack(void *pData, XnUInt32 nDataSize)
Definition: XnGeneralBuffer.h:51
XnStatus XnGeneralBufferCopy(XnGeneralBuffer *pDest, const XnGeneralBuffer *pSrc)
Definition: XnGeneralBuffer.h:62
struct XnGeneralBuffer XnGeneralBuffer
void XnGeneralBufferFree(XnGeneralBuffer *pDest)
Definition: XnGeneralBuffer.h:101
XnStatus XnGeneralBufferRealloc(XnGeneralBuffer *pDest, XnUInt32 nSize)
Definition: XnGeneralBuffer.h:88
XnUInt32 nDataSize
Definition: XnGeneralBuffer.h:41
#define XN_FREE_AND_NULL(x)
Definition: XnOS.h:150