Line data Source code
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 <sys/types.h>
18 :
19 : #include "AAtomizer.h"
20 :
21 : namespace stagefright {
22 :
23 : // static
24 3 : AAtomizer AAtomizer::gAtomizer;
25 :
26 : // static
27 0 : const char *AAtomizer::Atomize(const char *name) {
28 0 : return gAtomizer.atomize(name);
29 : }
30 :
31 3 : AAtomizer::AAtomizer() {
32 387 : for (size_t i = 0; i < 128; ++i) {
33 384 : mAtoms.push(List<AString>());
34 : }
35 3 : }
36 :
37 0 : const char *AAtomizer::atomize(const char *name) {
38 0 : Mutex::Autolock autoLock(mLock);
39 :
40 0 : const size_t n = mAtoms.size();
41 0 : size_t index = AAtomizer::Hash(name) % n;
42 0 : List<AString> &entry = mAtoms.editItemAt(index);
43 0 : List<AString>::iterator it = entry.begin();
44 0 : while (it != entry.end()) {
45 0 : if ((*it) == name) {
46 0 : return (*it).c_str();
47 : }
48 0 : ++it;
49 : }
50 :
51 0 : entry.push_back(AString(name));
52 :
53 0 : return (*--entry.end()).c_str();
54 : }
55 :
56 : // static
57 0 : uint32_t AAtomizer::Hash(const char *s) {
58 0 : uint32_t sum = 0;
59 0 : while (*s != '\0') {
60 0 : sum = (sum * 31) + *s;
61 0 : ++s;
62 : }
63 :
64 0 : return sum;
65 : }
66 :
67 : } // namespace stagefright
|