AVPKit
Buffer.cpp
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #include <cstring>
21 
22 #include <com/avpkit/ferry/Logger.h>
23 #include <com/avpkit/ferry/JNIMemoryManager.h>
24 
25 #include <com/avpkit/ferry/Buffer.h>
26 
27 VS_LOG_SETUP(VS_CPP_PACKAGE);
28 
29 namespace com { namespace avpkit { namespace ferry
30 {
31  uint8_t Buffer :: mTypeSize[] = {
32  // Must be in right order
33  sizeof(uint8_t),
34  sizeof(int8_t),
35  sizeof(uint16_t),
36  sizeof(int16_t),
37  sizeof(uint32_t),
38  sizeof(int32_t),
39  sizeof(uint64_t),
40  sizeof(int64_t),
41  sizeof(float),
42  sizeof(double),
43  0
44  };
45  Buffer :: Buffer() : mBuffer(0), mBufferSize(0)
46  {
47  mFreeFunc = 0;
48  mClosure = 0;
49  mInternallyAllocated = false;
50  mType = IBUFFER_UINT8; // bytes
51  }
52 
53  Buffer :: ~Buffer()
54  {
55  if (mBuffer)
56  {
57  VS_ASSERT(mBufferSize, "had buffer but no size");
58  if (mInternallyAllocated)
59  JNIMemoryManager::free(mBuffer);
60  else if (mFreeFunc)
61  mFreeFunc(mBuffer, mClosure);
62  mBuffer = 0;
63  mBufferSize = 0;
64  mFreeFunc = 0;
65  mClosure = 0;
66  }
67  }
68 
69 
70  void*
71  Buffer :: getBytes(int32_t offset, int32_t length)
72  {
73  void* retval = 0;
74 
75  if (length == 0)
76  length = mBufferSize - offset;
77 
78  if ((length > 0) && (length + offset <= mBufferSize))
79  retval = ((unsigned char*) mBuffer)+offset;
80 
81  return retval;
82  }
83 
84  int32_t
85  Buffer :: getBufferSize()
86  {
87  return mBufferSize;
88  }
89 
90  Buffer*
91  Buffer :: make(com::avpkit::ferry::RefCounted* requestor, int32_t bufferSize)
92  {
93  Buffer* retval = 0;
94  if (bufferSize <= 0)
95  return 0;
96 
97  void * allocator = requestor ? requestor->getJavaAllocator() : 0;
98  void *buffer = JNIMemoryManager::malloc(allocator, bufferSize);
99  if (!buffer)
100  return 0;
101 
102  retval = Buffer::make();
103  if (!retval) {
104  JNIMemoryManager::free(buffer);
105  return 0;
106  }
107  retval->mBuffer = buffer;
108  retval->mBufferSize = bufferSize;
109  retval->mInternallyAllocated = true;
110  return retval;
111  }
112 
113  Buffer*
114  Buffer :: make(com::avpkit::ferry::RefCounted* /*unused*/, void *bufToWrap, int32_t bufferSize,
115  FreeFunc freeFunc, void *closure)
116  {
117  Buffer * retval = 0;
118 
119  if (bufToWrap && bufferSize>0)
120  {
121  retval = Buffer::make();
122  if (retval)
123  {
124  retval->mFreeFunc = freeFunc;
125  retval->mClosure = closure;
126  retval->mBufferSize = bufferSize;
127  retval->mBuffer = bufToWrap;
128  retval->mInternallyAllocated = false;
129  }
130  }
131  return retval;
132  }
133 
134  IBuffer::Type
135  Buffer :: getType()
136  {
137  return mType;
138  }
139 
140  void
141  Buffer :: setType(Type type)
142  {
143  mType = type;
144  }
145 
146  Buffer*
147  Buffer :: make(com::avpkit::ferry::RefCounted* requestor,
148  Type type, int32_t numElements, bool zero)
149  {
150  if (numElements <= 0)
151  return 0;
152  if (type < 0 || type >= IBUFFER_NB)
153  return 0;
154 
155  int32_t bytesRequested = numElements*mTypeSize[(int32_t)type];
156  Buffer *retval = Buffer::make(requestor, bytesRequested);
157  if (retval)
158  {
159  retval->mType = type;
160  if (zero)
161  memset(retval->getBytes(0, bytesRequested), 0, bytesRequested);
162  }
163  return retval;
164  }
165 
166  int32_t
167  Buffer :: getTypeSize(Type type)
168  {
169  if (type < 0 || type >= IBUFFER_NB)
170  return 0;
171  return mTypeSize[(int32_t)type];
172  }
173 
174  int32_t
175  Buffer :: getSize()
176  {
177  if (mType < 0 || mType >= IBUFFER_NB)
178  return 0;
179  return getBufferSize()/mTypeSize[(int32_t)mType];
180  }
181 }}}
Parent of all Ferry objects – it mains reference counts in native code.
Definition: RefCounted.h:85
void * getJavaAllocator()
This method is public but not part of the standard API.
Definition: RefCounted.cpp:57
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...