1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18
19 #include "media_fragment.h"
20
21
MediaFragment(const MediaFragment & that,OsclAny * ptr,int32 len)22 OSCL_EXPORT_REF MediaFragment::MediaFragment(const MediaFragment& that, OsclAny* ptr, int32 len):
23 _fragment(that._fragment), _state(that._state)
24 {
25 if (_state)
26 _state->increment_refcnt();
27 if (ptr && (len > 0))
28 {
29 if ((ptr >= _fragment.ptr && ptr < ((uint8*)_fragment.ptr + _fragment.len)) &&
30 (((uint8*)ptr + len) <= ((uint8*)_fragment.ptr + _fragment.len)))
31 {
32 _fragment.ptr = ptr;
33 _fragment.len = len;
34 }
35 }
36 }
37
38 // Default fragment allocator. Just malloc's and free's memory as needed.
allocate_fragment(uint32 sz)39 OSCL_EXPORT_REF MediaFragment* DefFragmentAllocator::allocate_fragment(uint32 sz)
40 {
41 BufferFragment frag;
42 BufferState* state = NULL;
43
44 frag.ptr = OSCL_DEFAULT_MALLOC(sz);
45 frag.len = sz;
46 state = OSCL_NEW(BufferState, (&buffer_state_listener, frag.ptr));
47 return OSCL_NEW(MediaFragment, (frag, state));
48 }
49
allocate_fragment(MediaFragment * fragment,OsclAny * ptr,uint32 len)50 OSCL_EXPORT_REF MediaFragment* DefFragmentAllocator::allocate_fragment(MediaFragment* fragment,
51 OsclAny* ptr, uint32 len)
52 {
53 return OSCL_NEW(MediaFragment, (*fragment, ptr, len));
54 }
55
56
deallocate_fragment(MediaFragment * fragment)57 OSCL_EXPORT_REF int32 DefFragmentAllocator::deallocate_fragment(MediaFragment* fragment)
58 {
59 OSCL_DELETE(fragment);
60 return 1;
61 }
62
63
64
65 // Buffer pool manager. Frees the buffer back to the pool.
BufferReleased(OsclAny * ptr,BufferState * state)66 OSCL_EXPORT_REF OsclAny BufferPoolMgr::BufferReleased(OsclAny* ptr, BufferState* state)
67 {
68 OSCL_UNUSED_ARG(state);
69
70 BufferStateEntry *entry = (BufferStateEntry *) ptr;
71 pBufferPool->free_buffer_to_pool(entry);
72 }
73
74 // Buffer fragment pool. Memory is pre-allocated at constructor time.
PoolFragmentAllocator(int32 max_frags,int32 max_frag_size)75 OSCL_EXPORT_REF PoolFragmentAllocator::PoolFragmentAllocator(int32 max_frags, int32 max_frag_size)
76 {
77 buffer_state_listener = OSCL_NEW(BufferPoolMgr, (this));
78
79 num_frags = max_frags;
80 frag_size = max_frag_size;
81 entire_list = new BufferStateEntry[max_frags];
82 free_list = entire_list;
83 buffer = (uint8 *) OSCL_DEFAULT_MALLOC((max_frags * max_frag_size));
84 oscl_memset(buffer, 0, (max_frags * max_frag_size));
85
86 int32 i;
87 for (i = 0; i < max_frags; i++)
88 {
89 entire_list[i].buf_state.bind(&entire_list[i], buffer_state_listener);
90 entire_list[i].frag.ptr = buffer + (i * max_frag_size);
91 entire_list[i].frag.len = max_frag_size;
92 if (i > 0)
93 {
94 entire_list[i-1].next_entry = &entire_list[i];
95 }
96 }
97 entire_list[i-1].next_entry = NULL;
98 }
99
~PoolFragmentAllocator()100 OSCL_EXPORT_REF PoolFragmentAllocator::~PoolFragmentAllocator()
101 {
102 for (int32 i = 0; i < num_frags; i++)
103 {
104 if (entire_list[i].buf_state.get_refcount() > 0)
105 {
106 OSCL_ASSERT(0);
107 }
108 }
109
110 if (buffer) OSCL_DEFAULT_FREE(buffer);
111 if (entire_list) delete[] entire_list;
112 if (buffer_state_listener) OSCL_DELETE(buffer_state_listener);
113 }
114
allocate_fragment(uint32 sz)115 OSCL_EXPORT_REF MediaFragment* PoolFragmentAllocator::allocate_fragment(uint32 sz)
116 {
117 BufferStateEntry *entry;
118
119 if (free_list && ((int32) sz <= frag_size))
120 {
121 entry = free_list;
122 free_list = free_list->next_entry;
123
124 entry->next_entry = NULL;
125 entry->frag.len = sz;
126 entry->media_frag.Set(entry->frag, &entry->buf_state);
127
128 return &entry->media_frag;
129 }
130 else
131 {
132 return NULL;
133 }
134 }
135
allocate_fragment(MediaFragment * fragment,OsclAny * ptr,uint32 len)136 OSCL_EXPORT_REF MediaFragment* PoolFragmentAllocator::allocate_fragment(MediaFragment* fragment,
137 OsclAny* ptr,
138 uint32 len)
139 {
140 return OSCL_NEW(MediaFragment, (*fragment, ptr, len));
141 }
142
deallocate_fragment(MediaFragment * fragment)143 OSCL_EXPORT_REF int32 PoolFragmentAllocator::deallocate_fragment(MediaFragment* fragment)
144 {
145 fragment->Set(NULL, 0, NULL);
146 return 1;
147 }
148
free_buffer_to_pool(BufferStateEntry * entry)149 OsclAny PoolFragmentAllocator::free_buffer_to_pool(BufferStateEntry *entry)
150 {
151 entry->next_entry = free_list;
152 free_list = entry;
153 }
154
155
156