• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 #include "ABuffer.h"
18 
19 #include "ADebug.h"
20 #include "ALooper.h"
21 #include "AMessage.h"
22 
23 namespace android {
24 
ABuffer(size_t capacity)25 ABuffer::ABuffer(size_t capacity)
26     : mRangeOffset(0),
27       mInt32Data(0),
28       mOwnsData(true) {
29     mData = malloc(capacity);
30     if (mData == NULL) {
31         mCapacity = 0;
32         mRangeLength = 0;
33     } else {
34         mCapacity = capacity;
35         mRangeLength = capacity;
36     }
37 }
38 
ABuffer(void * data,size_t capacity)39 ABuffer::ABuffer(void *data, size_t capacity)
40     : mData(data),
41       mCapacity(capacity),
42       mRangeOffset(0),
43       mRangeLength(capacity),
44       mInt32Data(0),
45       mOwnsData(false) {
46 }
47 
48 // static
CreateAsCopy(const void * data,size_t capacity)49 sp<ABuffer> ABuffer::CreateAsCopy(const void *data, size_t capacity)
50 {
51     sp<ABuffer> res = new ABuffer(capacity);
52     if (res->base() == NULL) {
53         return NULL;
54     }
55     memcpy(res->data(), data, capacity);
56     return res;
57 }
58 
~ABuffer()59 ABuffer::~ABuffer() {
60     if (mOwnsData) {
61         if (mData != NULL) {
62             free(mData);
63             mData = NULL;
64         }
65     }
66 }
67 
setRange(size_t offset,size_t size)68 void ABuffer::setRange(size_t offset, size_t size) {
69     CHECK_LE(offset, mCapacity);
70     CHECK_LE(offset + size, mCapacity);
71 
72     mRangeOffset = offset;
73     mRangeLength = size;
74 }
75 
meta()76 sp<AMessage> ABuffer::meta() {
77     if (mMeta == NULL) {
78         mMeta = new AMessage;
79     }
80     return mMeta;
81 }
82 
83 }  // namespace android
84 
85