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
23namespace android {
24
25ABuffer::ABuffer(size_t capacity)
26    : mData(malloc(capacity)),
27      mCapacity(capacity),
28      mRangeOffset(0),
29      mRangeLength(capacity),
30      mInt32Data(0),
31      mOwnsData(true) {
32}
33
34ABuffer::ABuffer(void *data, size_t capacity)
35    : mData(data),
36      mCapacity(capacity),
37      mRangeOffset(0),
38      mRangeLength(capacity),
39      mInt32Data(0),
40      mOwnsData(false) {
41}
42
43ABuffer::~ABuffer() {
44    if (mOwnsData) {
45        if (mData != NULL) {
46            free(mData);
47            mData = NULL;
48        }
49    }
50
51    if (mFarewell != NULL) {
52        mFarewell->post();
53    }
54}
55
56void ABuffer::setRange(size_t offset, size_t size) {
57    CHECK_LE(offset, mCapacity);
58    CHECK_LE(offset + size, mCapacity);
59
60    mRangeOffset = offset;
61    mRangeLength = size;
62}
63
64void ABuffer::setFarewellMessage(const sp<AMessage> msg) {
65    mFarewell = msg;
66}
67
68sp<AMessage> ABuffer::meta() {
69    if (mMeta == NULL) {
70        mMeta = new AMessage;
71    }
72    return mMeta;
73}
74
75}  // namespace android
76
77