OpenNI 1.5.4
XnGeneralBuffer.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2011 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * OpenNI is free software: you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published *
10 * by the Free Software Foundation, either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * OpenNI is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
20 * *
21 ****************************************************************************/
22 #ifndef __XN_GENERAL_BUFFER_H__
23 #define __XN_GENERAL_BUFFER_H__
24 
25 //---------------------------------------------------------------------------
26 // Includes
27 //---------------------------------------------------------------------------
28 #include "XnPlatform.h"
29 #include "XnOS.h"
30 #include "XnStatusCodes.h"
31 
32 //---------------------------------------------------------------------------
33 // Types
34 //---------------------------------------------------------------------------
35 
36 /* Describes a general buffer. */
37 typedef struct XnGeneralBuffer
38 {
39  /* A pointer to the actual data. */
40  void* pData;
41  /* The size of the data in bytes. */
42  XnUInt32 nDataSize;
44 
45 //---------------------------------------------------------------------------
46 // Exported Functions
47 //---------------------------------------------------------------------------
48 
52 inline XnGeneralBuffer XnGeneralBufferPack(void* pData, XnUInt32 nDataSize)
53 {
54  XnGeneralBuffer result;
55  result.pData = pData;
56  result.nDataSize = nDataSize;
57  return result;
58 }
59 
64 {
65  XN_VALIDATE_INPUT_PTR(pDest);
67 
68  if (pSrc->nDataSize > pDest->nDataSize)
69  return XN_STATUS_OUTPUT_BUFFER_OVERFLOW;
70 
71  xnOSMemCopy(pDest->pData, pSrc->pData, pSrc->nDataSize);
72  pDest->nDataSize = pSrc->nDataSize;
73  return XN_STATUS_OK;
74 }
75 
76 inline XnStatus XnGeneralBufferAlloc(XnGeneralBuffer* pDest, XnUInt32 nSize)
77 {
78  XN_VALIDATE_INPUT_PTR(pDest);
79 
80  void* pData;
81  pData = xnOSMalloc(nSize);
82  XN_VALIDATE_ALLOC_PTR(pData);
83 
84  pDest->pData = pData;
85  pDest->nDataSize = nSize;
86  return XN_STATUS_OK;
87 }
88 
89 inline XnStatus XnGeneralBufferRealloc(XnGeneralBuffer* pDest, XnUInt32 nSize)
90 {
91  XN_VALIDATE_INPUT_PTR(pDest);
92 
93  void* pData;
94  pData = xnOSRealloc(pDest, nSize);
95  XN_VALIDATE_ALLOC_PTR(pData);
96 
97  pDest->pData = pData;
98  pDest->nDataSize = nSize;
99  return XN_STATUS_OK;
100 }
101 
102 inline void XnGeneralBufferFree(XnGeneralBuffer* pDest)
103 {
104  XN_FREE_AND_NULL(pDest->pData);
105  pDest->nDataSize = 0;
106 }
107 
108 //---------------------------------------------------------------------------
109 // Helper Macros
110 //---------------------------------------------------------------------------
111 #define XN_PACK_GENERAL_BUFFER(x) XnGeneralBufferPack(&x, sizeof(x))
112 
113 #define XN_VALIDATE_GENERAL_BUFFER_TYPE(gb, t) \
114  if ((gb).nDataSize != sizeof(t)) \
115  { \
116  return XN_STATUS_INVALID_BUFFER_SIZE; \
117  }
118 
119 #endif //__XN_GENERAL_BUFFER_H__
XnOS.h
XN_VALIDATE_ALLOC_PTR
#define XN_VALIDATE_ALLOC_PTR(x)
Definition: XnOS.h:127
XN_FREE_AND_NULL
#define XN_FREE_AND_NULL(x)
Definition: XnOS.h:150
XnStatusCodes.h
XN_STATUS_OK
#define XN_STATUS_OK
Definition: XnStatus.h:36
XnGeneralBufferRealloc
XnStatus XnGeneralBufferRealloc(XnGeneralBuffer *pDest, XnUInt32 nSize)
Definition: XnGeneralBuffer.h:88
xnOSMalloc
XN_C_API void *XN_C_DECL xnOSMalloc(const XnSizeT nAllocSize)
xnOSMemCopy
XN_C_API void XN_C_DECL xnOSMemCopy(void *pDest, const void *pSource, XnSizeT nCount)
XnStatus
XnUInt32 XnStatus
Definition: XnStatus.h:33
XnGeneralBufferFree
void XnGeneralBufferFree(XnGeneralBuffer *pDest)
Definition: XnGeneralBuffer.h:101
XnGeneralBufferPack
XnGeneralBuffer XnGeneralBufferPack(void *pData, XnUInt32 nDataSize)
Definition: XnGeneralBuffer.h:51
XnGeneralBuffer
Definition: XnGeneralBuffer.h:36
XnGeneralBufferCopy
XnStatus XnGeneralBufferCopy(XnGeneralBuffer *pDest, const XnGeneralBuffer *pSrc)
Definition: XnGeneralBuffer.h:62
XnGeneralBuffer
struct XnGeneralBuffer XnGeneralBuffer
xnOSRealloc
XN_C_API void *XN_C_DECL xnOSRealloc(void *pMemory, const XnSizeT nAllocSize)
XN_VALIDATE_INPUT_PTR
#define XN_VALIDATE_INPUT_PTR(x)
Definition: XnOS.h:122
XnGeneralBuffer::nDataSize
XnUInt32 nDataSize
Definition: XnGeneralBuffer.h:60
XnGeneralBufferAlloc
XnStatus XnGeneralBufferAlloc(XnGeneralBuffer *pDest, XnUInt32 nSize)
Definition: XnGeneralBuffer.h:75
XnPlatform.h
XnGeneralBuffer::pData
void * pData
Definition: XnGeneralBuffer.h:58