• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_BLOB_H
18 #define ANDROID_BLOB_H
19 
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 namespace android {
25 
26 // read only byte buffer like object
27 
28 class BlobReadOnly {
29 public:
BlobReadOnly(const void * data,size_t size,bool byReference)30     BlobReadOnly(const void *data, size_t size, bool byReference) :
31         mMem(byReference ? NULL : malloc(size)),
32         mData(byReference ? data : mMem),
33         mSize(size) {
34         if (!byReference) {
35             memcpy(mMem, data, size);
36         }
37     }
~BlobReadOnly()38     ~BlobReadOnly() {
39         free(mMem);
40     }
41 
42 private:
43           void * const mMem;
44 
45 public:
46     const void * const mData;
47           const size_t mSize;
48 };
49 
50 // read/write byte buffer like object
51 
52 class Blob {
53 public:
Blob(size_t size)54     Blob(size_t size) :
55         mData(malloc(size)),
56         mOffset(0),
57         mSize(size),
58         mMem(mData) { }
59 
60     // by reference
Blob(void * data,size_t size)61     Blob(void *data, size_t size) :
62         mData(data),
63         mOffset(0),
64         mSize(size),
65         mMem(NULL) { }
66 
~Blob()67     ~Blob() {
68         free(mMem);
69     }
70 
71     void * const mData;
72           size_t mOffset;
73     const size_t mSize;
74 
75 private:
76     void * const mMem;
77 };
78 
79 } // namespace android
80 
81 #endif // ANDROID_BLOB_H
82