├── README ├── libs ├── Makefile ├── binder │ ├── Binder.cpp │ ├── BpBinder.cpp │ ├── CursorWindow.cpp │ ├── IInterface.cpp │ ├── IMemory.cpp │ ├── IPCThreadState.cpp │ ├── IPermissionController.cpp │ ├── IServiceManager.cpp │ ├── Makefile │ ├── MemoryBase.cpp │ ├── MemoryDealer.cpp │ ├── MemoryHeapBase.cpp │ ├── MemoryHeapPmem.cpp │ ├── Parcel.cpp │ ├── PermissionCache.cpp │ ├── ProcessState.cpp │ └── Static.cpp ├── cutils │ ├── Makefile │ ├── atomic.c │ ├── native_handle.c │ ├── process_name.c │ └── threads.c ├── include │ ├── binder │ │ ├── Binder.h │ │ ├── BinderService.h │ │ ├── BpBinder.h │ │ ├── CursorWindow.h │ │ ├── IBinder.h │ │ ├── IInterface.h │ │ ├── IMemory.h │ │ ├── IPCThreadState.h │ │ ├── IPermissionController.h │ │ ├── IServiceManager.h │ │ ├── MemoryBase.h │ │ ├── MemoryDealer.h │ │ ├── MemoryHeapBase.h │ │ ├── MemoryHeapPmem.h │ │ ├── Parcel.h │ │ ├── PermissionCache.h │ │ └── ProcessState.h │ ├── cutils │ │ ├── atomic-inline.h │ │ ├── atomic-x86.h │ │ ├── atomic.h │ │ ├── compiler.h │ │ ├── log.h │ │ ├── native_handle.h │ │ ├── process_name.h │ │ ├── properties.h │ │ ├── sched_policy.h │ │ ├── threads.h │ │ └── uio.h │ ├── private │ │ ├── binder │ │ │ ├── Static.h │ │ │ └── binder_module.h │ │ └── utils │ │ │ └── Static.h │ └── utils │ │ ├── Atomic.h │ │ ├── BufferedTextOutput.h │ │ ├── CallStack.h │ │ ├── Debug.h │ │ ├── Endian.h │ │ ├── Errors.h │ │ ├── Flattenable.h │ │ ├── KeyedVector.h │ │ ├── Log.h │ │ ├── RefBase.h │ │ ├── SharedBuffer.h │ │ ├── Singleton.h │ │ ├── SortedVector.h │ │ ├── String16.h │ │ ├── String8.h │ │ ├── StrongPointer.h │ │ ├── SystemClock.h │ │ ├── TextOutput.h │ │ ├── Timers.h │ │ ├── TypeHelpers.h │ │ ├── Unicode.h │ │ ├── Vector.h │ │ ├── VectorImpl.h │ │ ├── misc.h │ │ └── threads.h └── utils │ ├── BufferedTextOutput.cpp │ ├── CallStack.cpp │ ├── Debug.cpp │ ├── Flattenable.cpp │ ├── Makefile │ ├── RefBase.cpp │ ├── SharedBuffer.cpp │ ├── Static.cpp │ ├── String16.cpp │ ├── String8.cpp │ ├── SystemClock.cpp │ ├── TextOutput.cpp │ ├── Threads.cpp │ ├── Timers.cpp │ ├── Unicode.cpp │ ├── VectorImpl.cpp │ └── misc.cpp ├── module ├── Makefile ├── binder.c ├── binder.h ├── deps.c ├── gen_deps.sh └── new │ ├── binder.c │ ├── binder.h │ ├── fast_slob.h │ ├── inst.h │ ├── msg_queue.c │ └── msg_queue.h ├── releases ├── binder-android-kernel-version-0.1-alpha.patch ├── binder-android-kernel-version-0.2.patch ├── binder-android-kernel-version-0.3.patch ├── binder-android-kernel-version-0.4.patch └── binder-android-kernel-version-0.5.patch ├── servicemanager ├── Makefile ├── bctest.c ├── binder.c ├── binder.h └── service_manager.c └── test ├── Makefile ├── binderAddInts.cpp ├── binder_tester.c ├── client.c ├── data-31jan2012.tgz └── server.c /libs/Makefile: -------------------------------------------------------------------------------- 1 | all: libbinder.a 2 | 3 | include cutils/Makefile 4 | include utils/Makefile 5 | include binder/Makefile 6 | 7 | CFLAGS := -I.. -Iinclude -DHAVE_PTHREADS -DHAVE_SYS_UIO_H -DHAVE_ENDIAN_H #-DINLINE_TRANSACTION_DATA 8 | 9 | libbinder.a: $(objects) 10 | ar cr $@ $^ 11 | 12 | clean: 13 | rm -f libbinder.a `find -name \*.o` 14 | 15 | %.o : %.cpp 16 | gcc $(CFLAGS) -c -o $@ $< 17 | %.o : %.c 18 | gcc $(CFLAGS) -c -o $@ $< 19 | -------------------------------------------------------------------------------- /libs/binder/IInterface.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 18 | 19 | namespace android { 20 | 21 | // --------------------------------------------------------------------------- 22 | 23 | IInterface::IInterface() 24 | : RefBase() { 25 | } 26 | 27 | IInterface::~IInterface() { 28 | } 29 | 30 | sp IInterface::asBinder() 31 | { 32 | return this ? onAsBinder() : NULL; 33 | } 34 | 35 | sp IInterface::asBinder() const 36 | { 37 | return this ? const_cast(this)->onAsBinder() : NULL; 38 | } 39 | 40 | // --------------------------------------------------------------------------- 41 | 42 | }; // namespace android 43 | -------------------------------------------------------------------------------- /libs/binder/IPermissionController.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | #define LOG_TAG "PermissionController" 18 | 19 | #include 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | namespace android { 29 | 30 | // ---------------------------------------------------------------------- 31 | 32 | class BpPermissionController : public BpInterface 33 | { 34 | public: 35 | BpPermissionController(const sp& impl) 36 | : BpInterface(impl) 37 | { 38 | } 39 | 40 | virtual bool checkPermission(const String16& permission, int32_t pid, int32_t uid) 41 | { 42 | Parcel data, reply; 43 | data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor()); 44 | data.writeString16(permission); 45 | data.writeInt32(pid); 46 | data.writeInt32(uid); 47 | remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply); 48 | // fail on exception 49 | if (reply.readExceptionCode() != 0) return 0; 50 | return reply.readInt32() != 0; 51 | } 52 | }; 53 | 54 | IMPLEMENT_META_INTERFACE(PermissionController, "android.os.IPermissionController"); 55 | 56 | // ---------------------------------------------------------------------- 57 | 58 | status_t BnPermissionController::onTransact( 59 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 60 | { 61 | //printf("PermissionController received: "); data.print(); 62 | switch(code) { 63 | case CHECK_PERMISSION_TRANSACTION: { 64 | CHECK_INTERFACE(IPermissionController, data, reply); 65 | String16 permission = data.readString16(); 66 | int32_t pid = data.readInt32(); 67 | int32_t uid = data.readInt32(); 68 | bool res = checkPermission(permission, pid, uid); 69 | reply->writeNoException(); 70 | reply->writeInt32(res ? 1 : 0); 71 | return NO_ERROR; 72 | } break; 73 | default: 74 | return BBinder::onTransact(code, data, reply, flags); 75 | } 76 | } 77 | 78 | }; // namespace android 79 | -------------------------------------------------------------------------------- /libs/binder/Makefile: -------------------------------------------------------------------------------- 1 | waste := \ 2 | CursorWindow.cpp \ 3 | IMemory.cpp \ 4 | MemoryDealer.cpp \ 5 | MemoryBase.cpp \ 6 | MemoryHeapBase.cpp \ 7 | MemoryHeapPmem.cpp 8 | 9 | binder_sources := \ 10 | Binder.cpp \ 11 | BpBinder.cpp \ 12 | IInterface.cpp \ 13 | IPCThreadState.cpp \ 14 | IPermissionController.cpp \ 15 | IServiceManager.cpp \ 16 | Parcel.cpp \ 17 | PermissionCache.cpp \ 18 | ProcessState.cpp \ 19 | Static.cpp 20 | 21 | objects += $(patsubst %.cpp,binder/%.o, $(binder_sources)) 22 | -------------------------------------------------------------------------------- /libs/binder/MemoryBase.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 | 18 | #include 19 | #include 20 | 21 | #include 22 | 23 | 24 | namespace android { 25 | 26 | // --------------------------------------------------------------------------- 27 | 28 | MemoryBase::MemoryBase(const sp& heap, 29 | ssize_t offset, size_t size) 30 | : mSize(size), mOffset(offset), mHeap(heap) 31 | { 32 | } 33 | 34 | sp MemoryBase::getMemory(ssize_t* offset, size_t* size) const 35 | { 36 | if (offset) *offset = mOffset; 37 | if (size) *size = mSize; 38 | return mHeap; 39 | } 40 | 41 | MemoryBase::~MemoryBase() 42 | { 43 | } 44 | 45 | // --------------------------------------------------------------------------- 46 | }; // namespace android 47 | -------------------------------------------------------------------------------- /libs/binder/MemoryHeapBase.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 | #define LOG_TAG "MemoryHeapBase" 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | #ifdef HAVE_ANDROID_OS 35 | #include 36 | #endif 37 | 38 | 39 | namespace android { 40 | 41 | // --------------------------------------------------------------------------- 42 | 43 | MemoryHeapBase::MemoryHeapBase() 44 | : mFD(-1), mSize(0), mBase(MAP_FAILED), 45 | mDevice(NULL), mNeedUnmap(false), mOffset(0) 46 | { 47 | } 48 | 49 | MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags, char const * name) 50 | : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags), 51 | mDevice(0), mNeedUnmap(false), mOffset(0) 52 | { 53 | const size_t pagesize = getpagesize(); 54 | size = ((size + pagesize-1) & ~(pagesize-1)); 55 | int fd = ashmem_create_region(name == NULL ? "MemoryHeapBase" : name, size); 56 | LOGE_IF(fd<0, "error creating ashmem region: %s", strerror(errno)); 57 | if (fd >= 0) { 58 | if (mapfd(fd, size) == NO_ERROR) { 59 | if (flags & READ_ONLY) { 60 | ashmem_set_prot_region(fd, PROT_READ); 61 | } 62 | } 63 | } 64 | } 65 | 66 | MemoryHeapBase::MemoryHeapBase(const char* device, size_t size, uint32_t flags) 67 | : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags), 68 | mDevice(0), mNeedUnmap(false), mOffset(0) 69 | { 70 | int open_flags = O_RDWR; 71 | if (flags & NO_CACHING) 72 | open_flags |= O_SYNC; 73 | 74 | int fd = open(device, open_flags); 75 | LOGE_IF(fd<0, "error opening %s: %s", device, strerror(errno)); 76 | if (fd >= 0) { 77 | const size_t pagesize = getpagesize(); 78 | size = ((size + pagesize-1) & ~(pagesize-1)); 79 | if (mapfd(fd, size) == NO_ERROR) { 80 | mDevice = device; 81 | } 82 | } 83 | } 84 | 85 | MemoryHeapBase::MemoryHeapBase(int fd, size_t size, uint32_t flags, uint32_t offset) 86 | : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags), 87 | mDevice(0), mNeedUnmap(false), mOffset(0) 88 | { 89 | const size_t pagesize = getpagesize(); 90 | size = ((size + pagesize-1) & ~(pagesize-1)); 91 | mapfd(dup(fd), size, offset); 92 | } 93 | 94 | status_t MemoryHeapBase::init(int fd, void *base, int size, int flags, const char* device) 95 | { 96 | if (mFD != -1) { 97 | return INVALID_OPERATION; 98 | } 99 | mFD = fd; 100 | mBase = base; 101 | mSize = size; 102 | mFlags = flags; 103 | mDevice = device; 104 | return NO_ERROR; 105 | } 106 | 107 | status_t MemoryHeapBase::mapfd(int fd, size_t size, uint32_t offset) 108 | { 109 | if (size == 0) { 110 | // try to figure out the size automatically 111 | #ifdef HAVE_ANDROID_OS 112 | // first try the PMEM ioctl 113 | pmem_region reg; 114 | int err = ioctl(fd, PMEM_GET_TOTAL_SIZE, ®); 115 | if (err == 0) 116 | size = reg.len; 117 | #endif 118 | if (size == 0) { // try fstat 119 | struct stat sb; 120 | if (fstat(fd, &sb) == 0) 121 | size = sb.st_size; 122 | } 123 | // if it didn't work, let mmap() fail. 124 | } 125 | 126 | if ((mFlags & DONT_MAP_LOCALLY) == 0) { 127 | void* base = (uint8_t*)mmap(0, size, 128 | PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset); 129 | if (base == MAP_FAILED) { 130 | LOGE("mmap(fd=%d, size=%u) failed (%s)", 131 | fd, uint32_t(size), strerror(errno)); 132 | close(fd); 133 | return -errno; 134 | } 135 | //LOGD("mmap(fd=%d, base=%p, size=%lu)", fd, base, size); 136 | mBase = base; 137 | mNeedUnmap = true; 138 | } else { 139 | mBase = 0; // not MAP_FAILED 140 | mNeedUnmap = false; 141 | } 142 | mFD = fd; 143 | mSize = size; 144 | mOffset = offset; 145 | return NO_ERROR; 146 | } 147 | 148 | MemoryHeapBase::~MemoryHeapBase() 149 | { 150 | dispose(); 151 | } 152 | 153 | void MemoryHeapBase::dispose() 154 | { 155 | int fd = android_atomic_or(-1, &mFD); 156 | if (fd >= 0) { 157 | if (mNeedUnmap) { 158 | //LOGD("munmap(fd=%d, base=%p, size=%lu)", fd, mBase, mSize); 159 | munmap(mBase, mSize); 160 | } 161 | mBase = 0; 162 | mSize = 0; 163 | close(fd); 164 | } 165 | } 166 | 167 | int MemoryHeapBase::getHeapID() const { 168 | return mFD; 169 | } 170 | 171 | void* MemoryHeapBase::getBase() const { 172 | return mBase; 173 | } 174 | 175 | size_t MemoryHeapBase::getSize() const { 176 | return mSize; 177 | } 178 | 179 | uint32_t MemoryHeapBase::getFlags() const { 180 | return mFlags; 181 | } 182 | 183 | const char* MemoryHeapBase::getDevice() const { 184 | return mDevice; 185 | } 186 | 187 | uint32_t MemoryHeapBase::getOffset() const { 188 | return mOffset; 189 | } 190 | 191 | // --------------------------------------------------------------------------- 192 | }; // namespace android 193 | -------------------------------------------------------------------------------- /libs/binder/PermissionCache.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 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 | #define LOG_TAG "PermissionCache" 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | namespace android { 27 | 28 | // ---------------------------------------------------------------------------- 29 | 30 | ANDROID_SINGLETON_STATIC_INSTANCE(PermissionCache) ; 31 | 32 | // ---------------------------------------------------------------------------- 33 | 34 | PermissionCache::PermissionCache() { 35 | } 36 | 37 | status_t PermissionCache::check(bool* granted, 38 | const String16& permission, uid_t uid) const { 39 | Mutex::Autolock _l(mLock); 40 | Entry e; 41 | e.name = permission; 42 | e.uid = uid; 43 | ssize_t index = mCache.indexOf(e); 44 | if (index >= 0) { 45 | *granted = mCache.itemAt(index).granted; 46 | return NO_ERROR; 47 | } 48 | return NAME_NOT_FOUND; 49 | } 50 | 51 | void PermissionCache::cache(const String16& permission, 52 | uid_t uid, bool granted) { 53 | Mutex::Autolock _l(mLock); 54 | Entry e; 55 | ssize_t index = mPermissionNamesPool.indexOf(permission); 56 | if (index > 0) { 57 | e.name = mPermissionNamesPool.itemAt(index); 58 | } else { 59 | mPermissionNamesPool.add(permission); 60 | e.name = permission; 61 | } 62 | // note, we don't need to store the pid, which is not actually used in 63 | // permission checks 64 | e.uid = uid; 65 | e.granted = granted; 66 | index = mCache.indexOf(e); 67 | if (index < 0) { 68 | mCache.add(e); 69 | } 70 | } 71 | 72 | void PermissionCache::purge() { 73 | Mutex::Autolock _l(mLock); 74 | mCache.clear(); 75 | } 76 | 77 | bool PermissionCache::checkCallingPermission(const String16& permission) { 78 | return PermissionCache::checkCallingPermission(permission, NULL, NULL); 79 | } 80 | 81 | bool PermissionCache::checkCallingPermission( 82 | const String16& permission, int32_t* outPid, int32_t* outUid) { 83 | IPCThreadState* ipcState = IPCThreadState::self(); 84 | pid_t pid = ipcState->getCallingPid(); 85 | uid_t uid = ipcState->getCallingUid(); 86 | if (outPid) *outPid = pid; 87 | if (outUid) *outUid = uid; 88 | return PermissionCache::checkPermission(permission, pid, uid); 89 | } 90 | 91 | bool PermissionCache::checkPermission( 92 | const String16& permission, pid_t pid, uid_t uid) { 93 | if ((uid == 0) || (pid == getpid())) { 94 | // root and ourselves is always okay 95 | return true; 96 | } 97 | 98 | PermissionCache& pc(PermissionCache::getInstance()); 99 | bool granted = false; 100 | if (pc.check(&granted, permission, uid) != NO_ERROR) { 101 | nsecs_t t = -systemTime(); 102 | granted = android::checkPermission(permission, pid, uid); 103 | t += systemTime(); 104 | LOGD("checking %s for uid=%d => %s (%d us)", 105 | String8(permission).string(), uid, 106 | granted?"granted":"denied", (int)ns2us(t)); 107 | pc.cache(permission, uid, granted); 108 | } 109 | return granted; 110 | } 111 | 112 | // --------------------------------------------------------------------------- 113 | }; // namespace android 114 | -------------------------------------------------------------------------------- /libs/binder/Static.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 | // All static variables go here, to control initialization and 18 | // destruction order in the library. 19 | 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | namespace android { 26 | 27 | // ------------ ProcessState.cpp 28 | 29 | Mutex gProcessMutex; 30 | sp gProcess; 31 | 32 | class LibUtilsIPCtStatics 33 | { 34 | public: 35 | LibUtilsIPCtStatics() 36 | { 37 | } 38 | 39 | ~LibUtilsIPCtStatics() 40 | { 41 | IPCThreadState::shutdown(); 42 | } 43 | }; 44 | 45 | static LibUtilsIPCtStatics gIPCStatics; 46 | 47 | // ------------ ServiceManager.cpp 48 | 49 | Mutex gDefaultServiceManagerLock; 50 | sp gDefaultServiceManager; 51 | sp gPermissionController; 52 | 53 | } // namespace android 54 | -------------------------------------------------------------------------------- /libs/cutils/Makefile: -------------------------------------------------------------------------------- 1 | cutils_sources := \ 2 | atomic.c \ 3 | native_handle.c \ 4 | process_name.c \ 5 | threads.c 6 | 7 | objects += $(patsubst %.c,cutils/%.o, $(cutils_sources)) 8 | -------------------------------------------------------------------------------- /libs/cutils/atomic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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 | #define inline 18 | #define ANDROID_SMP 1 19 | 20 | #include 21 | -------------------------------------------------------------------------------- /libs/cutils/native_handle.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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 | #define LOG_TAG "NativeHandle" 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | native_handle_t* native_handle_create(int numFds, int numInts) 29 | { 30 | native_handle_t* h = malloc( 31 | sizeof(native_handle_t) + sizeof(int)*(numFds+numInts)); 32 | 33 | h->version = sizeof(native_handle_t); 34 | h->numFds = numFds; 35 | h->numInts = numInts; 36 | return h; 37 | } 38 | 39 | int native_handle_delete(native_handle_t* h) 40 | { 41 | if (h) { 42 | if (h->version != sizeof(native_handle_t)) 43 | return -EINVAL; 44 | free(h); 45 | } 46 | return 0; 47 | } 48 | 49 | int native_handle_close(const native_handle_t* h) 50 | { 51 | if (h->version != sizeof(native_handle_t)) 52 | return -EINVAL; 53 | 54 | const int numFds = h->numFds; 55 | int i; 56 | for (i=0 ; idata[i]); 58 | } 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /libs/cutils/process_name.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #if defined(HAVE_PRCTL) 27 | #include 28 | #endif 29 | 30 | #define PROCESS_NAME_DEVICE "/tmp/process_name" 31 | 32 | static const char* process_name = "unknown"; 33 | static int running_in_emulator = -1; 34 | 35 | void set_process_name(const char* new_name) { 36 | char propBuf[PROPERTY_VALUE_MAX]; 37 | 38 | if (new_name == NULL) { 39 | return; 40 | } 41 | 42 | // We never free the old name. Someone else could be using it. 43 | int len = strlen(new_name); 44 | char* copy = (char*) malloc(len + 1); 45 | strcpy(copy, new_name); 46 | process_name = (const char*) copy; 47 | 48 | #if defined(HAVE_PRCTL) 49 | if (len < 16) { 50 | prctl(PR_SET_NAME, (unsigned long) new_name, 0, 0, 0); 51 | } else { 52 | prctl(PR_SET_NAME, (unsigned long) new_name + len - 15, 0, 0, 0); 53 | } 54 | #endif 55 | 56 | // If we know we are not running in the emulator, then return. 57 | if (running_in_emulator == 0) { 58 | return; 59 | } 60 | 61 | // If the "running_in_emulator" variable has not been initialized, 62 | // then do it now. 63 | if (running_in_emulator == -1) { 64 | property_get("ro.kernel.qemu", propBuf, ""); 65 | if (propBuf[0] == '1') { 66 | running_in_emulator = 1; 67 | } else { 68 | running_in_emulator = 0; 69 | return; 70 | } 71 | } 72 | 73 | // If the emulator was started with the "-trace file" command line option 74 | // then we want to record the process name in the trace even if we are 75 | // not currently tracing instructions (so that we will know the process 76 | // name when we do start tracing instructions). We do not need to execute 77 | // this code if we are just running in the emulator without the "-trace" 78 | // command line option, but we don't know that here and this function 79 | // isn't called frequently enough to bother optimizing that case. 80 | int fd = open(PROCESS_NAME_DEVICE, O_RDWR); 81 | if (fd < 0) 82 | return; 83 | write(fd, process_name, strlen(process_name) + 1); 84 | close(fd); 85 | } 86 | 87 | const char* get_process_name(void) { 88 | return process_name; 89 | } 90 | -------------------------------------------------------------------------------- /libs/cutils/threads.c: -------------------------------------------------------------------------------- 1 | /* libs/cutils/threads.c 2 | ** 3 | ** Copyright (C) 2007, The Android Open Source Project 4 | ** 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); 6 | ** you may not use this file except in compliance with the License. 7 | ** You may obtain a copy of the License at 8 | ** 9 | ** http://www.apache.org/licenses/LICENSE-2.0 10 | ** 11 | ** Unless required by applicable law or agreed to in writing, software 12 | ** distributed under the License is distributed on an "AS IS" BASIS, 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ** See the License for the specific language governing permissions and 15 | ** limitations under the License. 16 | */ 17 | #include 18 | 19 | #ifdef HAVE_PTHREADS 20 | void* thread_store_get( thread_store_t* store ) 21 | { 22 | const pthread_key_t k = store->tls; 23 | 24 | if (!store->has_tls) 25 | return NULL; 26 | 27 | return pthread_getspecific( store->tls ); 28 | } 29 | 30 | extern void thread_store_set( thread_store_t* store, 31 | void* value, 32 | thread_store_destruct_t destroy) 33 | { 34 | pthread_mutex_lock( &store->lock ); 35 | if (!store->has_tls) { 36 | if (pthread_key_create( &store->tls, destroy) != 0) { 37 | pthread_mutex_unlock(&store->lock); 38 | return; 39 | } 40 | store->has_tls = 1; 41 | } 42 | pthread_mutex_unlock( &store->lock ); 43 | 44 | pthread_setspecific( store->tls, value ); 45 | } 46 | 47 | #endif 48 | 49 | #ifdef HAVE_WIN32_THREADS 50 | void* thread_store_get( thread_store_t* store ) 51 | { 52 | if (!store->has_tls) 53 | return NULL; 54 | 55 | return (void*) TlsGetValue( store->tls ); 56 | } 57 | 58 | void thread_store_set( thread_store_t* store, 59 | void* value, 60 | thread_store_destruct_t destroy ) 61 | { 62 | /* XXX: can't use destructor on thread exit */ 63 | if (!store->lock_init) { 64 | store->lock_init = -1; 65 | InitializeCriticalSection( &store->lock ); 66 | store->lock_init = -2; 67 | } else while (store->lock_init != -2) { 68 | Sleep(10); /* 10ms */ 69 | } 70 | 71 | EnterCriticalSection( &store->lock ); 72 | if (!store->has_tls) { 73 | store->tls = TlsAlloc(); 74 | if (store->tls == TLS_OUT_OF_INDEXES) { 75 | LeaveCriticalSection( &store->lock ); 76 | return; 77 | } 78 | store->has_tls = 1; 79 | } 80 | LeaveCriticalSection( &store->lock ); 81 | 82 | TlsSetValue( store->tls, value ); 83 | } 84 | #endif 85 | -------------------------------------------------------------------------------- /libs/include/binder/Binder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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_BINDER_H 18 | #define ANDROID_BINDER_H 19 | 20 | #include 21 | 22 | // --------------------------------------------------------------------------- 23 | namespace android { 24 | 25 | class BBinder : public IBinder 26 | { 27 | public: 28 | BBinder(); 29 | 30 | virtual const String16& getInterfaceDescriptor() const; 31 | virtual bool isBinderAlive() const; 32 | virtual status_t pingBinder(); 33 | virtual status_t dump(int fd, const Vector& args); 34 | 35 | virtual status_t transact( uint32_t code, 36 | const Parcel& data, 37 | Parcel* reply, 38 | uint32_t flags = 0); 39 | 40 | virtual status_t linkToDeath(const sp& recipient, 41 | void* cookie = NULL, 42 | uint32_t flags = 0); 43 | 44 | virtual status_t unlinkToDeath( const wp& recipient, 45 | void* cookie = NULL, 46 | uint32_t flags = 0, 47 | wp* outRecipient = NULL); 48 | 49 | virtual void attachObject( const void* objectID, 50 | void* object, 51 | void* cleanupCookie, 52 | object_cleanup_func func); 53 | virtual void* findObject(const void* objectID) const; 54 | virtual void detachObject(const void* objectID); 55 | 56 | virtual BBinder* localBinder(); 57 | 58 | protected: 59 | virtual ~BBinder(); 60 | 61 | virtual status_t onTransact( uint32_t code, 62 | const Parcel& data, 63 | Parcel* reply, 64 | uint32_t flags = 0); 65 | 66 | private: 67 | BBinder(const BBinder& o); 68 | BBinder& operator=(const BBinder& o); 69 | 70 | class Extras; 71 | 72 | Extras* mExtras; 73 | void* mReserved0; 74 | }; 75 | 76 | // --------------------------------------------------------------------------- 77 | 78 | class BpRefBase : public virtual RefBase 79 | { 80 | protected: 81 | BpRefBase(const sp& o); 82 | virtual ~BpRefBase(); 83 | virtual void onFirstRef(); 84 | virtual void onLastStrongRef(const void* id); 85 | virtual bool onIncStrongAttempted(uint32_t flags, const void* id); 86 | 87 | inline IBinder* remote() { return mRemote; } 88 | inline IBinder* remote() const { return mRemote; } 89 | 90 | private: 91 | BpRefBase(const BpRefBase& o); 92 | BpRefBase& operator=(const BpRefBase& o); 93 | 94 | IBinder* const mRemote; 95 | RefBase::weakref_type* mRefs; 96 | volatile int32_t mState; 97 | }; 98 | 99 | }; // namespace android 100 | 101 | // --------------------------------------------------------------------------- 102 | 103 | #endif // ANDROID_BINDER_H 104 | -------------------------------------------------------------------------------- /libs/include/binder/BinderService.h: -------------------------------------------------------------------------------- 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 | #ifndef ANDROID_BINDER_SERVICE_H 18 | #define ANDROID_BINDER_SERVICE_H 19 | 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | // --------------------------------------------------------------------------- 31 | namespace android { 32 | 33 | template 34 | class BinderService 35 | { 36 | public: 37 | static status_t publish() { 38 | sp sm(defaultServiceManager()); 39 | return sm->addService(String16(SERVICE::getServiceName()), new SERVICE()); 40 | } 41 | 42 | static void publishAndJoinThreadPool() { 43 | sp proc(ProcessState::self()); 44 | sp sm(defaultServiceManager()); 45 | sm->addService(String16(SERVICE::getServiceName()), new SERVICE()); 46 | ProcessState::self()->startThreadPool(); 47 | IPCThreadState::self()->joinThreadPool(); 48 | } 49 | 50 | static void instantiate() { publish(); } 51 | 52 | static status_t shutdown() { 53 | return NO_ERROR; 54 | } 55 | }; 56 | 57 | 58 | }; // namespace android 59 | // --------------------------------------------------------------------------- 60 | #endif // ANDROID_BINDER_SERVICE_H 61 | -------------------------------------------------------------------------------- /libs/include/binder/BpBinder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_BPBINDER_H 18 | #define ANDROID_BPBINDER_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | // --------------------------------------------------------------------------- 25 | namespace android { 26 | 27 | class BpBinder : public IBinder 28 | { 29 | public: 30 | BpBinder(int32_t handle); 31 | 32 | inline int32_t handle() const { return mHandle; } 33 | 34 | virtual const String16& getInterfaceDescriptor() const; 35 | virtual bool isBinderAlive() const; 36 | virtual status_t pingBinder(); 37 | virtual status_t dump(int fd, const Vector& args); 38 | 39 | virtual status_t transact( uint32_t code, 40 | const Parcel& data, 41 | Parcel* reply, 42 | uint32_t flags = 0); 43 | 44 | virtual status_t linkToDeath(const sp& recipient, 45 | void* cookie = NULL, 46 | uint32_t flags = 0); 47 | virtual status_t unlinkToDeath( const wp& recipient, 48 | void* cookie = NULL, 49 | uint32_t flags = 0, 50 | wp* outRecipient = NULL); 51 | 52 | virtual void attachObject( const void* objectID, 53 | void* object, 54 | void* cleanupCookie, 55 | object_cleanup_func func); 56 | virtual void* findObject(const void* objectID) const; 57 | virtual void detachObject(const void* objectID); 58 | 59 | virtual BpBinder* remoteBinder(); 60 | 61 | status_t setConstantData(const void* data, size_t size); 62 | void sendObituary(); 63 | 64 | class ObjectManager 65 | { 66 | public: 67 | ObjectManager(); 68 | ~ObjectManager(); 69 | 70 | void attach( const void* objectID, 71 | void* object, 72 | void* cleanupCookie, 73 | IBinder::object_cleanup_func func); 74 | void* find(const void* objectID) const; 75 | void detach(const void* objectID); 76 | 77 | void kill(); 78 | 79 | private: 80 | ObjectManager(const ObjectManager&); 81 | ObjectManager& operator=(const ObjectManager&); 82 | 83 | struct entry_t 84 | { 85 | void* object; 86 | void* cleanupCookie; 87 | IBinder::object_cleanup_func func; 88 | }; 89 | 90 | KeyedVector mObjects; 91 | }; 92 | 93 | protected: 94 | virtual ~BpBinder(); 95 | virtual void onFirstRef(); 96 | virtual void onLastStrongRef(const void* id); 97 | virtual bool onIncStrongAttempted(uint32_t flags, const void* id); 98 | 99 | private: 100 | const int32_t mHandle; 101 | 102 | struct Obituary { 103 | wp recipient; 104 | void* cookie; 105 | uint32_t flags; 106 | }; 107 | 108 | void reportOneDeath(const Obituary& obit); 109 | bool isDescriptorCached() const; 110 | 111 | mutable Mutex mLock; 112 | volatile int32_t mAlive; 113 | volatile int32_t mObitsSent; 114 | Vector* mObituaries; 115 | ObjectManager mObjects; 116 | Parcel* mConstantData; 117 | mutable String16 mDescriptorCache; 118 | }; 119 | 120 | }; // namespace android 121 | 122 | // --------------------------------------------------------------------------- 123 | 124 | #endif // ANDROID_BPBINDER_H 125 | -------------------------------------------------------------------------------- /libs/include/binder/CursorWindow.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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__DATABASE_WINDOW_H 18 | #define _ANDROID__DATABASE_WINDOW_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #if LOG_NDEBUG 28 | 29 | #define IF_LOG_WINDOW() if (false) 30 | #define LOG_WINDOW(...) 31 | 32 | #else 33 | 34 | #define IF_LOG_WINDOW() IF_LOG(LOG_DEBUG, "CursorWindow") 35 | #define LOG_WINDOW(...) LOG(LOG_DEBUG, "CursorWindow", __VA_ARGS__) 36 | 37 | #endif 38 | 39 | namespace android { 40 | 41 | /** 42 | * This class stores a set of rows from a database in a buffer. The begining of the 43 | * window has first chunk of RowSlots, which are offsets to the row directory, followed by 44 | * an offset to the next chunk in a linked-list of additional chunk of RowSlots in case 45 | * the pre-allocated chunk isn't big enough to refer to all rows. Each row directory has a 46 | * FieldSlot per column, which has the size, offset, and type of the data for that field. 47 | * Note that the data types come from sqlite3.h. 48 | * 49 | * Strings are stored in UTF-8. 50 | */ 51 | class CursorWindow { 52 | CursorWindow(const String8& name, int ashmemFd, 53 | void* data, size_t size, bool readOnly); 54 | 55 | public: 56 | /* Field types. */ 57 | enum { 58 | FIELD_TYPE_NULL = 0, 59 | FIELD_TYPE_INTEGER = 1, 60 | FIELD_TYPE_FLOAT = 2, 61 | FIELD_TYPE_STRING = 3, 62 | FIELD_TYPE_BLOB = 4, 63 | }; 64 | 65 | /* Opaque type that describes a field slot. */ 66 | struct FieldSlot { 67 | private: 68 | int32_t type; 69 | union { 70 | double d; 71 | int64_t l; 72 | struct { 73 | uint32_t offset; 74 | uint32_t size; 75 | } buffer; 76 | } data; 77 | 78 | friend class CursorWindow; 79 | } __attribute((packed)); 80 | 81 | ~CursorWindow(); 82 | 83 | static status_t create(const String8& name, size_t size, CursorWindow** outCursorWindow); 84 | static status_t createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow); 85 | 86 | status_t writeToParcel(Parcel* parcel); 87 | 88 | inline String8 name() { return mName; } 89 | inline size_t size() { return mSize; } 90 | inline size_t freeSpace() { return mSize - mHeader->freeOffset; } 91 | inline uint32_t getNumRows() { return mHeader->numRows; } 92 | inline uint32_t getNumColumns() { return mHeader->numColumns; } 93 | 94 | status_t clear(); 95 | status_t setNumColumns(uint32_t numColumns); 96 | 97 | /** 98 | * Allocate a row slot and its directory. 99 | * The row is initialized will null entries for each field. 100 | */ 101 | status_t allocRow(); 102 | status_t freeLastRow(); 103 | 104 | status_t putBlob(uint32_t row, uint32_t column, const void* value, size_t size); 105 | status_t putString(uint32_t row, uint32_t column, const char* value, size_t sizeIncludingNull); 106 | status_t putLong(uint32_t row, uint32_t column, int64_t value); 107 | status_t putDouble(uint32_t row, uint32_t column, double value); 108 | status_t putNull(uint32_t row, uint32_t column); 109 | 110 | /** 111 | * Gets the field slot at the specified row and column. 112 | * Returns null if the requested row or column is not in the window. 113 | */ 114 | FieldSlot* getFieldSlot(uint32_t row, uint32_t column); 115 | 116 | inline int32_t getFieldSlotType(FieldSlot* fieldSlot) { 117 | return fieldSlot->type; 118 | } 119 | 120 | inline int64_t getFieldSlotValueLong(FieldSlot* fieldSlot) { 121 | return fieldSlot->data.l; 122 | } 123 | 124 | inline double getFieldSlotValueDouble(FieldSlot* fieldSlot) { 125 | return fieldSlot->data.d; 126 | } 127 | 128 | inline const char* getFieldSlotValueString(FieldSlot* fieldSlot, 129 | size_t* outSizeIncludingNull) { 130 | *outSizeIncludingNull = fieldSlot->data.buffer.size; 131 | return static_cast(offsetToPtr(fieldSlot->data.buffer.offset)); 132 | } 133 | 134 | inline const void* getFieldSlotValueBlob(FieldSlot* fieldSlot, size_t* outSize) { 135 | *outSize = fieldSlot->data.buffer.size; 136 | return offsetToPtr(fieldSlot->data.buffer.offset); 137 | } 138 | 139 | private: 140 | static const size_t ROW_SLOT_CHUNK_NUM_ROWS = 100; 141 | 142 | struct Header { 143 | // Offset of the lowest unused byte in the window. 144 | uint32_t freeOffset; 145 | 146 | // Offset of the first row slot chunk. 147 | uint32_t firstChunkOffset; 148 | 149 | uint32_t numRows; 150 | uint32_t numColumns; 151 | }; 152 | 153 | struct RowSlot { 154 | uint32_t offset; 155 | }; 156 | 157 | struct RowSlotChunk { 158 | RowSlot slots[ROW_SLOT_CHUNK_NUM_ROWS]; 159 | uint32_t nextChunkOffset; 160 | }; 161 | 162 | String8 mName; 163 | int mAshmemFd; 164 | void* mData; 165 | size_t mSize; 166 | bool mReadOnly; 167 | Header* mHeader; 168 | 169 | inline void* offsetToPtr(uint32_t offset) { 170 | return static_cast(mData) + offset; 171 | } 172 | 173 | inline uint32_t offsetFromPtr(void* ptr) { 174 | return static_cast(ptr) - static_cast(mData); 175 | } 176 | 177 | /** 178 | * Allocate a portion of the window. Returns the offset 179 | * of the allocation, or 0 if there isn't enough space. 180 | * If aligned is true, the allocation gets 4 byte alignment. 181 | */ 182 | uint32_t alloc(size_t size, bool aligned = false); 183 | 184 | RowSlot* getRowSlot(uint32_t row); 185 | RowSlot* allocRowSlot(); 186 | 187 | status_t putBlobOrString(uint32_t row, uint32_t column, 188 | const void* value, size_t size, int32_t type); 189 | }; 190 | 191 | }; // namespace android 192 | 193 | #endif 194 | -------------------------------------------------------------------------------- /libs/include/binder/IBinder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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_IBINDER_H 18 | #define ANDROID_IBINDER_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | #define B_PACK_CHARS(c1, c2, c3, c4) \ 27 | ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4)) 28 | 29 | // --------------------------------------------------------------------------- 30 | namespace android { 31 | 32 | class BBinder; 33 | class BpBinder; 34 | class IInterface; 35 | class Parcel; 36 | 37 | /** 38 | * Base class and low-level protocol for a remotable object. 39 | * You can derive from this class to create an object for which other 40 | * processes can hold references to it. Communication between processes 41 | * (method calls, property get and set) is down through a low-level 42 | * protocol implemented on top of the transact() API. 43 | */ 44 | class IBinder : public virtual RefBase 45 | { 46 | public: 47 | enum { 48 | FIRST_CALL_TRANSACTION = 0x00000001, 49 | LAST_CALL_TRANSACTION = 0x00ffffff, 50 | 51 | PING_TRANSACTION = B_PACK_CHARS('_','P','N','G'), 52 | DUMP_TRANSACTION = B_PACK_CHARS('_','D','M','P'), 53 | INTERFACE_TRANSACTION = B_PACK_CHARS('_', 'N', 'T', 'F'), 54 | 55 | // Corresponds to TF_ONE_WAY -- an asynchronous call. 56 | FLAG_ONEWAY = 0x00000001 57 | }; 58 | 59 | IBinder(); 60 | 61 | /** 62 | * Check if this IBinder implements the interface named by 63 | * @a descriptor. If it does, the base pointer to it is returned, 64 | * which you can safely static_cast<> to the concrete C++ interface. 65 | */ 66 | virtual sp queryLocalInterface(const String16& descriptor); 67 | 68 | /** 69 | * Return the canonical name of the interface provided by this IBinder 70 | * object. 71 | */ 72 | virtual const String16& getInterfaceDescriptor() const = 0; 73 | 74 | virtual bool isBinderAlive() const = 0; 75 | virtual status_t pingBinder() = 0; 76 | virtual status_t dump(int fd, const Vector& args) = 0; 77 | 78 | virtual status_t transact( uint32_t code, 79 | const Parcel& data, 80 | Parcel* reply, 81 | uint32_t flags = 0) = 0; 82 | 83 | /** 84 | * This method allows you to add data that is transported through 85 | * IPC along with your IBinder pointer. When implementing a Binder 86 | * object, override it to write your desired data in to @a outData. 87 | * You can then call getConstantData() on your IBinder to retrieve 88 | * that data, from any process. You MUST return the number of bytes 89 | * written in to the parcel (including padding). 90 | */ 91 | class DeathRecipient : public virtual RefBase 92 | { 93 | public: 94 | virtual void binderDied(const wp& who) = 0; 95 | }; 96 | 97 | /** 98 | * Register the @a recipient for a notification if this binder 99 | * goes away. If this binder object unexpectedly goes away 100 | * (typically because its hosting process has been killed), 101 | * then DeathRecipient::binderDied() will be called with a reference 102 | * to this. 103 | * 104 | * The @a cookie is optional -- if non-NULL, it should be a 105 | * memory address that you own (that is, you know it is unique). 106 | * 107 | * @note You will only receive death notifications for remote binders, 108 | * as local binders by definition can't die without you dying as well. 109 | * Trying to use this function on a local binder will result in an 110 | * INVALID_OPERATION code being returned and nothing happening. 111 | * 112 | * @note This link always holds a weak reference to its recipient. 113 | * 114 | * @note You will only receive a weak reference to the dead 115 | * binder. You should not try to promote this to a strong reference. 116 | * (Nor should you need to, as there is nothing useful you can 117 | * directly do with it now that it has passed on.) 118 | */ 119 | virtual status_t linkToDeath(const sp& recipient, 120 | void* cookie = NULL, 121 | uint32_t flags = 0) = 0; 122 | 123 | /** 124 | * Remove a previously registered death notification. 125 | * The @a recipient will no longer be called if this object 126 | * dies. The @a cookie is optional. If non-NULL, you can 127 | * supply a NULL @a recipient, and the recipient previously 128 | * added with that cookie will be unlinked. 129 | */ 130 | virtual status_t unlinkToDeath( const wp& recipient, 131 | void* cookie = NULL, 132 | uint32_t flags = 0, 133 | wp* outRecipient = NULL) = 0; 134 | 135 | virtual bool checkSubclass(const void* subclassID) const; 136 | 137 | typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie); 138 | 139 | virtual void attachObject( const void* objectID, 140 | void* object, 141 | void* cleanupCookie, 142 | object_cleanup_func func) = 0; 143 | virtual void* findObject(const void* objectID) const = 0; 144 | virtual void detachObject(const void* objectID) = 0; 145 | 146 | virtual BBinder* localBinder(); 147 | virtual BpBinder* remoteBinder(); 148 | 149 | protected: 150 | virtual ~IBinder(); 151 | 152 | private: 153 | }; 154 | 155 | }; // namespace android 156 | 157 | // --------------------------------------------------------------------------- 158 | 159 | #endif // ANDROID_IBINDER_H 160 | -------------------------------------------------------------------------------- /libs/include/binder/IInterface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // 18 | #ifndef ANDROID_IINTERFACE_H 19 | #define ANDROID_IINTERFACE_H 20 | 21 | #include 22 | 23 | namespace android { 24 | 25 | // ---------------------------------------------------------------------- 26 | 27 | class IInterface : public virtual RefBase 28 | { 29 | public: 30 | IInterface(); 31 | sp asBinder(); 32 | sp asBinder() const; 33 | 34 | protected: 35 | virtual ~IInterface(); 36 | virtual IBinder* onAsBinder() = 0; 37 | }; 38 | 39 | // ---------------------------------------------------------------------- 40 | 41 | template 42 | inline sp interface_cast(const sp& obj) 43 | { 44 | return INTERFACE::asInterface(obj); 45 | } 46 | 47 | // ---------------------------------------------------------------------- 48 | 49 | template 50 | class BnInterface : public INTERFACE, public BBinder 51 | { 52 | public: 53 | virtual sp queryLocalInterface(const String16& _descriptor); 54 | virtual const String16& getInterfaceDescriptor() const; 55 | 56 | protected: 57 | virtual IBinder* onAsBinder(); 58 | }; 59 | 60 | // ---------------------------------------------------------------------- 61 | 62 | template 63 | class BpInterface : public INTERFACE, public BpRefBase 64 | { 65 | public: 66 | BpInterface(const sp& remote); 67 | 68 | protected: 69 | virtual IBinder* onAsBinder(); 70 | }; 71 | 72 | // ---------------------------------------------------------------------- 73 | 74 | #define DECLARE_META_INTERFACE(INTERFACE) \ 75 | static const android::String16 descriptor; \ 76 | static android::sp asInterface( \ 77 | const android::sp& obj); \ 78 | virtual const android::String16& getInterfaceDescriptor() const; \ 79 | I##INTERFACE(); \ 80 | virtual ~I##INTERFACE(); \ 81 | 82 | 83 | #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ 84 | const android::String16 I##INTERFACE::descriptor(NAME); \ 85 | const android::String16& \ 86 | I##INTERFACE::getInterfaceDescriptor() const { \ 87 | return I##INTERFACE::descriptor; \ 88 | } \ 89 | android::sp I##INTERFACE::asInterface( \ 90 | const android::sp& obj) \ 91 | { \ 92 | android::sp intr; \ 93 | if (obj != NULL) { \ 94 | intr = static_cast( \ 95 | obj->queryLocalInterface( \ 96 | I##INTERFACE::descriptor).get()); \ 97 | if (intr == NULL) { \ 98 | intr = new Bp##INTERFACE(obj); \ 99 | } \ 100 | } \ 101 | return intr; \ 102 | } \ 103 | I##INTERFACE::I##INTERFACE() { } \ 104 | I##INTERFACE::~I##INTERFACE() { } \ 105 | 106 | 107 | #define CHECK_INTERFACE(interface, data, reply) \ 108 | if (!data.checkInterface(this)) { return PERMISSION_DENIED; } \ 109 | 110 | 111 | // ---------------------------------------------------------------------- 112 | // No user-serviceable parts after this... 113 | 114 | template 115 | inline sp BnInterface::queryLocalInterface( 116 | const String16& _descriptor) 117 | { 118 | if (_descriptor == INTERFACE::descriptor) return this; 119 | return NULL; 120 | } 121 | 122 | template 123 | inline const String16& BnInterface::getInterfaceDescriptor() const 124 | { 125 | return INTERFACE::getInterfaceDescriptor(); 126 | } 127 | 128 | template 129 | IBinder* BnInterface::onAsBinder() 130 | { 131 | return this; 132 | } 133 | 134 | template 135 | inline BpInterface::BpInterface(const sp& remote) 136 | : BpRefBase(remote) 137 | { 138 | } 139 | 140 | template 141 | inline IBinder* BpInterface::onAsBinder() 142 | { 143 | return remote(); 144 | } 145 | 146 | // ---------------------------------------------------------------------- 147 | 148 | }; // namespace android 149 | 150 | #endif // ANDROID_IINTERFACE_H 151 | -------------------------------------------------------------------------------- /libs/include/binder/IMemory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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_IMEMORY_H 18 | #define ANDROID_IMEMORY_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | namespace android { 29 | 30 | // ---------------------------------------------------------------------------- 31 | 32 | class IMemoryHeap : public IInterface 33 | { 34 | public: 35 | DECLARE_META_INTERFACE(MemoryHeap); 36 | 37 | // flags returned by getFlags() 38 | enum { 39 | READ_ONLY = 0x00000001 40 | }; 41 | 42 | virtual int getHeapID() const = 0; 43 | virtual void* getBase() const = 0; 44 | virtual size_t getSize() const = 0; 45 | virtual uint32_t getFlags() const = 0; 46 | virtual uint32_t getOffset() const = 0; 47 | 48 | // these are there just for backward source compatibility 49 | int32_t heapID() const { return getHeapID(); } 50 | void* base() const { return getBase(); } 51 | size_t virtualSize() const { return getSize(); } 52 | }; 53 | 54 | class BnMemoryHeap : public BnInterface 55 | { 56 | public: 57 | virtual status_t onTransact( 58 | uint32_t code, 59 | const Parcel& data, 60 | Parcel* reply, 61 | uint32_t flags = 0); 62 | 63 | BnMemoryHeap(); 64 | protected: 65 | virtual ~BnMemoryHeap(); 66 | }; 67 | 68 | // ---------------------------------------------------------------------------- 69 | 70 | class IMemory : public IInterface 71 | { 72 | public: 73 | DECLARE_META_INTERFACE(Memory); 74 | 75 | virtual sp getMemory(ssize_t* offset=0, size_t* size=0) const = 0; 76 | 77 | // helpers 78 | void* fastPointer(const sp& heap, ssize_t offset) const; 79 | void* pointer() const; 80 | size_t size() const; 81 | ssize_t offset() const; 82 | }; 83 | 84 | class BnMemory : public BnInterface 85 | { 86 | public: 87 | virtual status_t onTransact( 88 | uint32_t code, 89 | const Parcel& data, 90 | Parcel* reply, 91 | uint32_t flags = 0); 92 | 93 | BnMemory(); 94 | protected: 95 | virtual ~BnMemory(); 96 | }; 97 | 98 | // ---------------------------------------------------------------------------- 99 | 100 | }; // namespace android 101 | 102 | #endif // ANDROID_IMEMORY_H 103 | -------------------------------------------------------------------------------- /libs/include/binder/IPCThreadState.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_IPC_THREAD_STATE_H 18 | #define ANDROID_IPC_THREAD_STATE_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #ifdef HAVE_WIN32_PROC 26 | typedef int uid_t; 27 | #endif 28 | 29 | // --------------------------------------------------------------------------- 30 | namespace android { 31 | 32 | class IPCThreadState 33 | { 34 | public: 35 | static IPCThreadState* self(); 36 | static IPCThreadState* selfOrNull(); // self(), but won't instantiate 37 | 38 | sp process(); 39 | 40 | status_t clearLastError(); 41 | 42 | int getCallingPid(); 43 | int getCallingUid(); 44 | 45 | void setStrictModePolicy(int32_t policy); 46 | int32_t getStrictModePolicy() const; 47 | 48 | void setLastTransactionBinderFlags(int32_t flags); 49 | int32_t getLastTransactionBinderFlags() const; 50 | 51 | int64_t clearCallingIdentity(); 52 | void restoreCallingIdentity(int64_t token); 53 | 54 | void flushCommands(); 55 | 56 | void joinThreadPool(bool isMain = true); 57 | 58 | // Stop the local process. 59 | void stopProcess(bool immediate = true); 60 | 61 | status_t transact(int32_t handle, 62 | uint32_t code, const Parcel& data, 63 | Parcel* reply, uint32_t flags); 64 | 65 | void incStrongHandle(int32_t handle); 66 | void decStrongHandle(int32_t handle); 67 | void incWeakHandle(int32_t handle); 68 | void decWeakHandle(int32_t handle); 69 | status_t attemptIncStrongHandle(int32_t handle); 70 | static void expungeHandle(int32_t handle, IBinder* binder); 71 | status_t requestDeathNotification( int32_t handle, 72 | BpBinder* proxy); 73 | status_t clearDeathNotification( int32_t handle, 74 | BpBinder* proxy); 75 | 76 | static void shutdown(); 77 | 78 | // Call this to disable switching threads to background scheduling when 79 | // receiving incoming IPC calls. This is specifically here for the 80 | // Android system process, since it expects to have background apps calling 81 | // in to it but doesn't want to acquire locks in its services while in 82 | // the background. 83 | static void disableBackgroundScheduling(bool disable); 84 | 85 | private: 86 | IPCThreadState(); 87 | ~IPCThreadState(); 88 | 89 | status_t sendReply(const Parcel& reply, uint32_t flags); 90 | status_t waitForResponse(Parcel *reply, 91 | status_t *acquireResult=NULL); 92 | status_t talkWithDriver(bool doReceive=true); 93 | status_t writeTransactionData(int32_t cmd, 94 | uint32_t binderFlags, 95 | int32_t handle, 96 | uint32_t code, 97 | const Parcel& data, 98 | status_t* statusBuffer); 99 | status_t executeCommand(int32_t command); 100 | 101 | void clearCaller(); 102 | 103 | static void threadDestructor(void *st); 104 | static void freeBuffer(Parcel* parcel, 105 | const uint8_t* data, size_t dataSize, 106 | const size_t* objects, size_t objectsSize, 107 | void* cookie); 108 | 109 | const sp mProcess; 110 | const pid_t mMyThreadId; 111 | Vector mPendingStrongDerefs; 112 | Vector mPendingWeakDerefs; 113 | 114 | Parcel mIn; 115 | Parcel mOut; 116 | status_t mLastError; 117 | pid_t mCallingPid; 118 | uid_t mCallingUid; 119 | int32_t mStrictModePolicy; 120 | int32_t mLastTransactionBinderFlags; 121 | }; 122 | 123 | }; // namespace android 124 | 125 | // --------------------------------------------------------------------------- 126 | 127 | #endif // ANDROID_IPC_THREAD_STATE_H 128 | -------------------------------------------------------------------------------- /libs/include/binder/IPermissionController.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // 18 | #ifndef ANDROID_IPERMISSION_CONTROLLER_H 19 | #define ANDROID_IPERMISSION_CONTROLLER_H 20 | 21 | #include 22 | 23 | namespace android { 24 | 25 | // ---------------------------------------------------------------------- 26 | 27 | class IPermissionController : public IInterface 28 | { 29 | public: 30 | DECLARE_META_INTERFACE(PermissionController); 31 | 32 | virtual bool checkPermission(const String16& permission, 33 | int32_t pid, int32_t uid) = 0; 34 | 35 | enum { 36 | CHECK_PERMISSION_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION 37 | }; 38 | }; 39 | 40 | // ---------------------------------------------------------------------- 41 | 42 | class BnPermissionController : public BnInterface 43 | { 44 | public: 45 | virtual status_t onTransact( uint32_t code, 46 | const Parcel& data, 47 | Parcel* reply, 48 | uint32_t flags = 0); 49 | }; 50 | 51 | // ---------------------------------------------------------------------- 52 | 53 | }; // namespace android 54 | 55 | #endif // ANDROID_IPERMISSION_CONTROLLER_H 56 | 57 | -------------------------------------------------------------------------------- /libs/include/binder/IServiceManager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // 18 | #ifndef ANDROID_ISERVICE_MANAGER_H 19 | #define ANDROID_ISERVICE_MANAGER_H 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | namespace android { 27 | 28 | // ---------------------------------------------------------------------- 29 | 30 | class IServiceManager : public IInterface 31 | { 32 | public: 33 | DECLARE_META_INTERFACE(ServiceManager); 34 | 35 | /** 36 | * Retrieve an existing service, blocking for a few seconds 37 | * if it doesn't yet exist. 38 | */ 39 | virtual sp getService( const String16& name) const = 0; 40 | 41 | /** 42 | * Retrieve an existing service, non-blocking. 43 | */ 44 | virtual sp checkService( const String16& name) const = 0; 45 | 46 | /** 47 | * Register a service. 48 | */ 49 | virtual status_t addService( const String16& name, 50 | const sp& service) = 0; 51 | 52 | /** 53 | * Return list of all existing services. 54 | */ 55 | virtual Vector listServices() = 0; 56 | 57 | enum { 58 | GET_SERVICE_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION, 59 | CHECK_SERVICE_TRANSACTION, 60 | ADD_SERVICE_TRANSACTION, 61 | LIST_SERVICES_TRANSACTION, 62 | }; 63 | }; 64 | 65 | sp defaultServiceManager(); 66 | 67 | template 68 | status_t getService(const String16& name, sp* outService) 69 | { 70 | const sp sm = defaultServiceManager(); 71 | if (sm != NULL) { 72 | *outService = interface_cast(sm->getService(name)); 73 | if ((*outService) != NULL) return NO_ERROR; 74 | } 75 | return NAME_NOT_FOUND; 76 | } 77 | 78 | bool checkCallingPermission(const String16& permission); 79 | bool checkCallingPermission(const String16& permission, 80 | int32_t* outPid, int32_t* outUid); 81 | bool checkPermission(const String16& permission, pid_t pid, uid_t uid); 82 | 83 | 84 | // ---------------------------------------------------------------------- 85 | 86 | class BnServiceManager : public BnInterface 87 | { 88 | public: 89 | virtual status_t onTransact( uint32_t code, 90 | const Parcel& data, 91 | Parcel* reply, 92 | uint32_t flags = 0); 93 | }; 94 | 95 | // ---------------------------------------------------------------------- 96 | 97 | }; // namespace android 98 | 99 | #endif // ANDROID_ISERVICE_MANAGER_H 100 | 101 | -------------------------------------------------------------------------------- /libs/include/binder/MemoryBase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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_MEMORY_BASE_H 18 | #define ANDROID_MEMORY_BASE_H 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | 26 | namespace android { 27 | 28 | // --------------------------------------------------------------------------- 29 | 30 | class MemoryBase : public BnMemory 31 | { 32 | public: 33 | MemoryBase(const sp& heap, ssize_t offset, size_t size); 34 | virtual ~MemoryBase(); 35 | virtual sp getMemory(ssize_t* offset, size_t* size) const; 36 | 37 | protected: 38 | size_t getSize() const { return mSize; } 39 | ssize_t getOffset() const { return mOffset; } 40 | const sp& getHeap() const { return mHeap; } 41 | 42 | private: 43 | size_t mSize; 44 | ssize_t mOffset; 45 | sp mHeap; 46 | }; 47 | 48 | // --------------------------------------------------------------------------- 49 | }; // namespace android 50 | 51 | #endif // ANDROID_MEMORY_BASE_H 52 | -------------------------------------------------------------------------------- /libs/include/binder/MemoryDealer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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_MEMORY_DEALER_H 18 | #define ANDROID_MEMORY_DEALER_H 19 | 20 | 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | namespace android { 28 | // ---------------------------------------------------------------------------- 29 | 30 | class SimpleBestFitAllocator; 31 | 32 | // ---------------------------------------------------------------------------- 33 | 34 | class MemoryDealer : public RefBase 35 | { 36 | public: 37 | MemoryDealer(size_t size, const char* name = 0); 38 | 39 | virtual sp allocate(size_t size); 40 | virtual void deallocate(size_t offset); 41 | virtual void dump(const char* what) const; 42 | 43 | sp getMemoryHeap() const { return heap(); } 44 | 45 | protected: 46 | virtual ~MemoryDealer(); 47 | 48 | private: 49 | const sp& heap() const; 50 | SimpleBestFitAllocator* allocator() const; 51 | 52 | sp mHeap; 53 | SimpleBestFitAllocator* mAllocator; 54 | }; 55 | 56 | 57 | // ---------------------------------------------------------------------------- 58 | }; // namespace android 59 | 60 | #endif // ANDROID_MEMORY_DEALER_H 61 | -------------------------------------------------------------------------------- /libs/include/binder/MemoryHeapBase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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_MEMORY_HEAP_BASE_H 18 | #define ANDROID_MEMORY_HEAP_BASE_H 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | 26 | namespace android { 27 | 28 | // --------------------------------------------------------------------------- 29 | 30 | class MemoryHeapBase : public virtual BnMemoryHeap 31 | { 32 | public: 33 | enum { 34 | READ_ONLY = IMemoryHeap::READ_ONLY, 35 | // memory won't be mapped locally, but will be mapped in the remote 36 | // process. 37 | DONT_MAP_LOCALLY = 0x00000100, 38 | NO_CACHING = 0x00000200 39 | }; 40 | 41 | /* 42 | * maps the memory referenced by fd. but DOESN'T take ownership 43 | * of the filedescriptor (it makes a copy with dup() 44 | */ 45 | MemoryHeapBase(int fd, size_t size, uint32_t flags = 0, uint32_t offset = 0); 46 | 47 | /* 48 | * maps memory from the given device 49 | */ 50 | MemoryHeapBase(const char* device, size_t size = 0, uint32_t flags = 0); 51 | 52 | /* 53 | * maps memory from ashmem, with the given name for debugging 54 | */ 55 | MemoryHeapBase(size_t size, uint32_t flags = 0, char const* name = NULL); 56 | 57 | virtual ~MemoryHeapBase(); 58 | 59 | /* implement IMemoryHeap interface */ 60 | virtual int getHeapID() const; 61 | virtual void* getBase() const; 62 | virtual size_t getSize() const; 63 | virtual uint32_t getFlags() const; 64 | virtual uint32_t getOffset() const; 65 | 66 | const char* getDevice() const; 67 | 68 | /* this closes this heap -- use carefully */ 69 | void dispose(); 70 | 71 | /* this is only needed as a workaround, use only if you know 72 | * what you are doing */ 73 | status_t setDevice(const char* device) { 74 | if (mDevice == 0) 75 | mDevice = device; 76 | return mDevice ? NO_ERROR : ALREADY_EXISTS; 77 | } 78 | 79 | protected: 80 | MemoryHeapBase(); 81 | // init() takes ownership of fd 82 | status_t init(int fd, void *base, int size, 83 | int flags = 0, const char* device = NULL); 84 | 85 | private: 86 | status_t mapfd(int fd, size_t size, uint32_t offset = 0); 87 | 88 | int mFD; 89 | size_t mSize; 90 | void* mBase; 91 | uint32_t mFlags; 92 | const char* mDevice; 93 | bool mNeedUnmap; 94 | uint32_t mOffset; 95 | }; 96 | 97 | // --------------------------------------------------------------------------- 98 | }; // namespace android 99 | 100 | #endif // ANDROID_MEMORY_HEAP_BASE_H 101 | -------------------------------------------------------------------------------- /libs/include/binder/MemoryHeapPmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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_MEMORY_HEAP_PMEM_H 18 | #define ANDROID_MEMORY_HEAP_PMEM_H 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | namespace android { 29 | 30 | class MemoryHeapBase; 31 | 32 | // --------------------------------------------------------------------------- 33 | 34 | class MemoryHeapPmem : public MemoryHeapBase 35 | { 36 | public: 37 | class MemoryPmem : public BnMemory { 38 | public: 39 | MemoryPmem(const sp& heap); 40 | ~MemoryPmem(); 41 | protected: 42 | const sp& getHeap() const { return mClientHeap; } 43 | private: 44 | friend class MemoryHeapPmem; 45 | virtual void revoke() = 0; 46 | sp mClientHeap; 47 | }; 48 | 49 | MemoryHeapPmem(const sp& pmemHeap, uint32_t flags = 0); 50 | ~MemoryHeapPmem(); 51 | 52 | /* HeapInterface additions */ 53 | virtual sp mapMemory(size_t offset, size_t size); 54 | 55 | /* make the whole heap visible (you know who you are) */ 56 | virtual status_t slap(); 57 | 58 | /* hide (revoke) the whole heap (the client will see the garbage page) */ 59 | virtual status_t unslap(); 60 | 61 | /* revoke all allocations made by this heap */ 62 | virtual void revoke(); 63 | 64 | private: 65 | /* use this to create your own IMemory for mapMemory */ 66 | virtual sp createMemory(size_t offset, size_t size); 67 | void remove(const wp& memory); 68 | 69 | private: 70 | sp mParentHeap; 71 | mutable Mutex mLock; 72 | SortedVector< wp > mAllocations; 73 | }; 74 | 75 | 76 | // --------------------------------------------------------------------------- 77 | }; // namespace android 78 | 79 | #endif // ANDROID_MEMORY_HEAP_PMEM_H 80 | -------------------------------------------------------------------------------- /libs/include/binder/PermissionCache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 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 BINDER_PERMISSION_H 18 | #define BINDER_PERMISSION_H 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | namespace android { 27 | // --------------------------------------------------------------------------- 28 | 29 | /* 30 | * PermissionCache caches permission checks for a given uid. 31 | * 32 | * Currently the cache is not updated when there is a permission change, 33 | * for instance when an application is uninstalled. 34 | * 35 | * IMPORTANT: for the reason stated above, only system permissions are safe 36 | * to cache. This restriction may be lifted at a later time. 37 | * 38 | */ 39 | 40 | class PermissionCache : Singleton { 41 | struct Entry { 42 | String16 name; 43 | uid_t uid; 44 | bool granted; 45 | inline bool operator < (const Entry& e) const { 46 | return (uid == e.uid) ? (name < e.name) : (uid < e.uid); 47 | } 48 | }; 49 | mutable Mutex mLock; 50 | // we pool all the permission names we see, as many permissions checks 51 | // will have identical names 52 | SortedVector< String16 > mPermissionNamesPool; 53 | // this is our cache per say. it stores pooled names. 54 | SortedVector< Entry > mCache; 55 | 56 | // free the whole cache, but keep the permission name pool 57 | void purge(); 58 | 59 | status_t check(bool* granted, 60 | const String16& permission, uid_t uid) const; 61 | 62 | void cache(const String16& permission, uid_t uid, bool granted); 63 | 64 | public: 65 | PermissionCache(); 66 | 67 | static bool checkCallingPermission(const String16& permission); 68 | 69 | static bool checkCallingPermission(const String16& permission, 70 | int32_t* outPid, int32_t* outUid); 71 | 72 | static bool checkPermission(const String16& permission, 73 | pid_t pid, uid_t uid); 74 | }; 75 | 76 | // --------------------------------------------------------------------------- 77 | }; // namespace android 78 | 79 | #endif /* BINDER_PERMISSION_H */ 80 | -------------------------------------------------------------------------------- /libs/include/binder/ProcessState.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_PROCESS_STATE_H 18 | #define ANDROID_PROCESS_STATE_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | // --------------------------------------------------------------------------- 28 | namespace android { 29 | 30 | // Global variables 31 | extern int mArgC; 32 | extern const char* const* mArgV; 33 | extern int mArgLen; 34 | 35 | class IPCThreadState; 36 | 37 | class ProcessState : public virtual RefBase 38 | { 39 | public: 40 | static sp self(); 41 | 42 | void setContextObject(const sp& object); 43 | sp getContextObject(const sp& caller); 44 | 45 | void setContextObject(const sp& object, 46 | const String16& name); 47 | sp getContextObject(const String16& name, 48 | const sp& caller); 49 | 50 | void startThreadPool(); 51 | 52 | typedef bool (*context_check_func)(const String16& name, 53 | const sp& caller, 54 | void* userData); 55 | 56 | bool isContextManager(void) const; 57 | bool becomeContextManager( 58 | context_check_func checkFunc, 59 | void* userData); 60 | 61 | sp getStrongProxyForHandle(int32_t handle); 62 | wp getWeakProxyForHandle(int32_t handle); 63 | void expungeHandle(int32_t handle, IBinder* binder); 64 | 65 | void setArgs(int argc, const char* const argv[]); 66 | int getArgC() const; 67 | const char* const* getArgV() const; 68 | 69 | void setArgV0(const char* txt); 70 | 71 | void spawnPooledThread(bool isMain); 72 | 73 | private: 74 | friend class IPCThreadState; 75 | 76 | ProcessState(); 77 | ~ProcessState(); 78 | 79 | ProcessState(const ProcessState& o); 80 | ProcessState& operator=(const ProcessState& o); 81 | 82 | struct handle_entry { 83 | IBinder* binder; 84 | RefBase::weakref_type* refs; 85 | }; 86 | 87 | handle_entry* lookupHandleLocked(int32_t handle); 88 | 89 | int mDriverFD; 90 | void* mVMStart; 91 | 92 | mutable Mutex mLock; // protects everything below. 93 | 94 | VectormHandleToObject; 95 | 96 | bool mManagesContexts; 97 | context_check_func mBinderContextCheckFunc; 98 | void* mBinderContextUserData; 99 | 100 | KeyedVector > 101 | mContexts; 102 | 103 | 104 | String8 mRootDir; 105 | bool mThreadPoolStarted; 106 | volatile int32_t mThreadPoolSeq; 107 | }; 108 | 109 | }; // namespace android 110 | 111 | // --------------------------------------------------------------------------- 112 | 113 | #endif // ANDROID_PROCESS_STATE_H 114 | -------------------------------------------------------------------------------- /libs/include/cutils/atomic-inline.h: -------------------------------------------------------------------------------- 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 | #ifndef ANDROID_CUTILS_ATOMIC_INLINE_H 18 | #define ANDROID_CUTILS_ATOMIC_INLINE_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* 25 | * Inline declarations and macros for some special-purpose atomic 26 | * operations. These are intended for rare circumstances where a 27 | * memory barrier needs to be issued inline rather than as a function 28 | * call. 29 | * 30 | * Most code should not use these. 31 | * 32 | * Anything that does include this file must set ANDROID_SMP to either 33 | * 0 or 1, indicating compilation for UP or SMP, respectively. 34 | * 35 | * Macros defined in this header: 36 | * 37 | * void ANDROID_MEMBAR_FULL(void) 38 | * Full memory barrier. Provides a compiler reordering barrier, and 39 | * on SMP systems emits an appropriate instruction. 40 | */ 41 | 42 | #if !defined(ANDROID_SMP) 43 | # error "Must define ANDROID_SMP before including atomic-inline.h" 44 | #endif 45 | 46 | #if defined(__arm__) 47 | #include 48 | #elif defined(__i386__) || defined(__x86_64__) 49 | #include 50 | #elif defined(__sh__) 51 | /* implementation is in atomic-android-sh.c */ 52 | #else 53 | #error atomic operations are unsupported 54 | #endif 55 | 56 | #if ANDROID_SMP == 0 57 | #define ANDROID_MEMBAR_FULL android_compiler_barrier 58 | #else 59 | #define ANDROID_MEMBAR_FULL android_memory_barrier 60 | #endif 61 | 62 | #if ANDROID_SMP == 0 63 | #define ANDROID_MEMBAR_STORE android_compiler_barrier 64 | #else 65 | #define ANDROID_MEMBAR_STORE android_memory_store_barrier 66 | #endif 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif 71 | 72 | #endif /* ANDROID_CUTILS_ATOMIC_INLINE_H */ 73 | -------------------------------------------------------------------------------- /libs/include/cutils/atomic-x86.h: -------------------------------------------------------------------------------- 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 | #ifndef ANDROID_CUTILS_ATOMIC_X86_H 18 | #define ANDROID_CUTILS_ATOMIC_X86_H 19 | 20 | #include 21 | 22 | extern inline void android_compiler_barrier(void) 23 | { 24 | __asm__ __volatile__ ("" : : : "memory"); 25 | } 26 | 27 | #if ANDROID_SMP == 0 28 | extern inline void android_memory_barrier(void) 29 | { 30 | android_compiler_barrier(); 31 | } 32 | extern inline void android_memory_store_barrier(void) 33 | { 34 | android_compiler_barrier(); 35 | } 36 | #else 37 | extern inline void android_memory_barrier(void) 38 | { 39 | __asm__ __volatile__ ("mfence" : : : "memory"); 40 | } 41 | extern inline void android_memory_store_barrier(void) 42 | { 43 | android_compiler_barrier(); 44 | } 45 | #endif 46 | 47 | extern inline int32_t android_atomic_acquire_load(volatile const int32_t *ptr) 48 | { 49 | int32_t value = *ptr; 50 | android_compiler_barrier(); 51 | return value; 52 | } 53 | 54 | extern inline int32_t android_atomic_release_load(volatile const int32_t *ptr) 55 | { 56 | android_memory_barrier(); 57 | return *ptr; 58 | } 59 | 60 | extern inline void android_atomic_acquire_store(int32_t value, 61 | volatile int32_t *ptr) 62 | { 63 | *ptr = value; 64 | android_memory_barrier(); 65 | } 66 | 67 | extern inline void android_atomic_release_store(int32_t value, 68 | volatile int32_t *ptr) 69 | { 70 | android_compiler_barrier(); 71 | *ptr = value; 72 | } 73 | 74 | extern inline int android_atomic_cas(int32_t old_value, int32_t new_value, 75 | volatile int32_t *ptr) 76 | { 77 | int32_t prev; 78 | __asm__ __volatile__ ("lock; cmpxchgl %1, %2" 79 | : "=a" (prev) 80 | : "q" (new_value), "m" (*ptr), "0" (old_value) 81 | : "memory"); 82 | return prev != old_value; 83 | } 84 | 85 | extern inline int android_atomic_acquire_cas(int32_t old_value, 86 | int32_t new_value, 87 | volatile int32_t *ptr) 88 | { 89 | /* Loads are not reordered with other loads. */ 90 | return android_atomic_cas(old_value, new_value, ptr); 91 | } 92 | 93 | extern inline int android_atomic_release_cas(int32_t old_value, 94 | int32_t new_value, 95 | volatile int32_t *ptr) 96 | { 97 | /* Stores are not reordered with other stores. */ 98 | return android_atomic_cas(old_value, new_value, ptr); 99 | } 100 | 101 | extern inline int32_t android_atomic_add(int32_t increment, 102 | volatile int32_t *ptr) 103 | { 104 | __asm__ __volatile__ ("lock; xaddl %0, %1" 105 | : "+r" (increment), "+m" (*ptr) 106 | : : "memory"); 107 | /* increment now holds the old value of *ptr */ 108 | return increment; 109 | } 110 | 111 | extern inline int32_t android_atomic_inc(volatile int32_t *addr) 112 | { 113 | return android_atomic_add(1, addr); 114 | } 115 | 116 | extern inline int32_t android_atomic_dec(volatile int32_t *addr) 117 | { 118 | return android_atomic_add(-1, addr); 119 | } 120 | 121 | extern inline int32_t android_atomic_and(int32_t value, 122 | volatile int32_t *ptr) 123 | { 124 | int32_t prev, status; 125 | do { 126 | prev = *ptr; 127 | status = android_atomic_cas(prev, prev & value, ptr); 128 | } while (__builtin_expect(status != 0, 0)); 129 | return prev; 130 | } 131 | 132 | extern inline int32_t android_atomic_or(int32_t value, volatile int32_t *ptr) 133 | { 134 | int32_t prev, status; 135 | do { 136 | prev = *ptr; 137 | status = android_atomic_cas(prev, prev | value, ptr); 138 | } while (__builtin_expect(status != 0, 0)); 139 | return prev; 140 | } 141 | 142 | #endif /* ANDROID_CUTILS_ATOMIC_X86_H */ 143 | -------------------------------------------------------------------------------- /libs/include/cutils/atomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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_CUTILS_ATOMIC_H 18 | #define ANDROID_CUTILS_ATOMIC_H 19 | 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | /* 28 | * A handful of basic atomic operations. The appropriate pthread 29 | * functions should be used instead of these whenever possible. 30 | * 31 | * The "acquire" and "release" terms can be defined intuitively in terms 32 | * of the placement of memory barriers in a simple lock implementation: 33 | * - wait until compare-and-swap(lock-is-free --> lock-is-held) succeeds 34 | * - barrier 35 | * - [do work] 36 | * - barrier 37 | * - store(lock-is-free) 38 | * In very crude terms, the initial (acquire) barrier prevents any of the 39 | * "work" from happening before the lock is held, and the later (release) 40 | * barrier ensures that all of the work happens before the lock is released. 41 | * (Think of cached writes, cache read-ahead, and instruction reordering 42 | * around the CAS and store instructions.) 43 | * 44 | * The barriers must apply to both the compiler and the CPU. Note it is 45 | * legal for instructions that occur before an "acquire" barrier to be 46 | * moved down below it, and for instructions that occur after a "release" 47 | * barrier to be moved up above it. 48 | * 49 | * The ARM-driven implementation we use here is short on subtlety, 50 | * and actually requests a full barrier from the compiler and the CPU. 51 | * The only difference between acquire and release is in whether they 52 | * are issued before or after the atomic operation with which they 53 | * are associated. To ease the transition to C/C++ atomic intrinsics, 54 | * you should not rely on this, and instead assume that only the minimal 55 | * acquire/release protection is provided. 56 | * 57 | * NOTE: all int32_t* values are expected to be aligned on 32-bit boundaries. 58 | * If they are not, atomicity is not guaranteed. 59 | */ 60 | 61 | /* 62 | * Basic arithmetic and bitwise operations. These all provide a 63 | * barrier with "release" ordering, and return the previous value. 64 | * 65 | * These have the same characteristics (e.g. what happens on overflow) 66 | * as the equivalent non-atomic C operations. 67 | */ 68 | int32_t android_atomic_inc(volatile int32_t* addr); 69 | int32_t android_atomic_dec(volatile int32_t* addr); 70 | int32_t android_atomic_add(int32_t value, volatile int32_t* addr); 71 | int32_t android_atomic_and(int32_t value, volatile int32_t* addr); 72 | int32_t android_atomic_or(int32_t value, volatile int32_t* addr); 73 | 74 | /* 75 | * Perform an atomic load with "acquire" or "release" ordering. 76 | * 77 | * This is only necessary if you need the memory barrier. A 32-bit read 78 | * from a 32-bit aligned address is atomic on all supported platforms. 79 | */ 80 | int32_t android_atomic_acquire_load(volatile const int32_t* addr); 81 | int32_t android_atomic_release_load(volatile const int32_t* addr); 82 | 83 | /* 84 | * Perform an atomic store with "acquire" or "release" ordering. 85 | * 86 | * This is only necessary if you need the memory barrier. A 32-bit write 87 | * to a 32-bit aligned address is atomic on all supported platforms. 88 | */ 89 | void android_atomic_acquire_store(int32_t value, volatile int32_t* addr); 90 | void android_atomic_release_store(int32_t value, volatile int32_t* addr); 91 | 92 | /* 93 | * Compare-and-set operation with "acquire" or "release" ordering. 94 | * 95 | * This returns zero if the new value was successfully stored, which will 96 | * only happen when *addr == oldvalue. 97 | * 98 | * (The return value is inverted from implementations on other platforms, 99 | * but matches the ARM ldrex/strex result.) 100 | * 101 | * Implementations that use the release CAS in a loop may be less efficient 102 | * than possible, because we re-issue the memory barrier on each iteration. 103 | */ 104 | int android_atomic_acquire_cas(int32_t oldvalue, int32_t newvalue, 105 | volatile int32_t* addr); 106 | int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue, 107 | volatile int32_t* addr); 108 | 109 | /* 110 | * Aliases for code using an older version of this header. These are now 111 | * deprecated and should not be used. The definitions will be removed 112 | * in a future release. 113 | */ 114 | #define android_atomic_write android_atomic_release_store 115 | #define android_atomic_cmpxchg android_atomic_release_cas 116 | 117 | #ifdef __cplusplus 118 | } // extern "C" 119 | #endif 120 | 121 | #endif // ANDROID_CUTILS_ATOMIC_H 122 | -------------------------------------------------------------------------------- /libs/include/cutils/compiler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 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_CUTILS_COMPILER_H 18 | #define ANDROID_CUTILS_COMPILER_H 19 | 20 | /* 21 | * helps the compiler's optimizer predicting branches 22 | */ 23 | 24 | #ifdef __cplusplus 25 | # define CC_LIKELY( exp ) (__builtin_expect( !!(exp), true )) 26 | # define CC_UNLIKELY( exp ) (__builtin_expect( !!(exp), false )) 27 | #else 28 | # define CC_LIKELY( exp ) (__builtin_expect( !!(exp), 1 )) 29 | # define CC_UNLIKELY( exp ) (__builtin_expect( !!(exp), 0 )) 30 | #endif 31 | 32 | /** 33 | * exports marked symbols 34 | * 35 | * if used on a C++ class declaration, this macro must be inserted 36 | * after the "class" keyword. For instance: 37 | * 38 | * template 39 | * class ANDROID_API Singleton { } 40 | */ 41 | 42 | #define ANDROID_API __attribute__((visibility("default"))) 43 | 44 | #endif // ANDROID_CUTILS_COMPILER_H 45 | -------------------------------------------------------------------------------- /libs/include/cutils/native_handle.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 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 NATIVE_HANDLE_H_ 18 | #define NATIVE_HANDLE_H_ 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | typedef struct native_handle 25 | { 26 | int version; /* sizeof(native_handle_t) */ 27 | int numFds; /* number of file-descriptors at &data[0] */ 28 | int numInts; /* number of ints at &data[numFds] */ 29 | int data[0]; /* numFds + numInts ints */ 30 | } native_handle_t; 31 | 32 | /* 33 | * native_handle_close 34 | * 35 | * closes the file descriptors contained in this native_handle_t 36 | * 37 | * return 0 on success, or a negative error code on failure 38 | * 39 | */ 40 | int native_handle_close(const native_handle_t* h); 41 | 42 | 43 | /* 44 | * native_handle_create 45 | * 46 | * creates a native_handle_t and initializes it. must be destroyed with 47 | * native_handle_delete(). 48 | * 49 | */ 50 | native_handle_t* native_handle_create(int numFds, int numInts); 51 | 52 | /* 53 | * native_handle_delete 54 | * 55 | * frees a native_handle_t allocated with native_handle_create(). 56 | * This ONLY frees the memory allocated for the native_handle_t, but doesn't 57 | * close the file descriptors; which can be achieved with native_handle_close(). 58 | * 59 | * return 0 on success, or a negative error code on failure 60 | * 61 | */ 62 | int native_handle_delete(native_handle_t* h); 63 | 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif /* NATIVE_HANDLE_H_ */ 70 | -------------------------------------------------------------------------------- /libs/include/cutils/process_name.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 | /** 18 | * Gives the current process a name. 19 | */ 20 | 21 | #ifndef __PROCESS_NAME_H 22 | #define __PROCESS_NAME_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /** 29 | * Sets the current process name. 30 | * 31 | * Warning: This leaks a string every time you call it. Use judiciously! 32 | */ 33 | void set_process_name(const char* process_name); 34 | 35 | /** Gets the current process name. */ 36 | const char* get_process_name(void); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif /* __PROCESS_NAME_H */ 43 | -------------------------------------------------------------------------------- /libs/include/cutils/properties.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 __CUTILS_PROPERTIES_H 18 | #define __CUTILS_PROPERTIES_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* System properties are *small* name value pairs managed by the 25 | ** property service. If your data doesn't fit in the provided 26 | ** space it is not appropriate for a system property. 27 | ** 28 | ** WARNING: system/bionic/include/sys/system_properties.h also defines 29 | ** these, but with different names. (TODO: fix that) 30 | */ 31 | #define PROPERTY_KEY_MAX 32 32 | #define PROPERTY_VALUE_MAX 92 33 | 34 | /* property_get: returns the length of the value which will never be 35 | ** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated. 36 | ** (the length does not include the terminating zero). 37 | ** 38 | ** If the property read fails or returns an empty value, the default 39 | ** value is used (if nonnull). 40 | */ 41 | inline int property_get(const char *key, char *value, const char *default_value) { *value = '\0'; return 0; } 42 | 43 | /* property_set: returns 0 on success, < 0 on failure 44 | */ 45 | int property_set(const char *key, const char *value); 46 | 47 | int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie); 48 | 49 | 50 | #ifdef HAVE_SYSTEM_PROPERTY_SERVER 51 | /* 52 | * We have an external property server instead of built-in libc support. 53 | * Used by the simulator. 54 | */ 55 | #define SYSTEM_PROPERTY_PIPE_NAME "/tmp/android-sysprop" 56 | 57 | enum { 58 | kSystemPropertyUnknown = 0, 59 | kSystemPropertyGet, 60 | kSystemPropertySet, 61 | kSystemPropertyList 62 | }; 63 | #endif /*HAVE_SYSTEM_PROPERTY_SERVER*/ 64 | 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /libs/include/cutils/sched_policy.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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 __CUTILS_SCHED_POLICY_H 18 | #define __CUTILS_SCHED_POLICY_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | typedef enum { 25 | SP_BACKGROUND = 0, 26 | SP_FOREGROUND = 1, 27 | } SchedPolicy; 28 | 29 | inline int set_sched_policy(int tid, SchedPolicy policy) {} 30 | inline int get_sched_policy(int tid, SchedPolicy *policy) { return -1; } 31 | 32 | #ifdef __cplusplus 33 | } 34 | #endif 35 | 36 | #endif /* __CUTILS_SCHED_POLICY_H */ 37 | -------------------------------------------------------------------------------- /libs/include/cutils/threads.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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 _LIBS_CUTILS_THREADS_H 18 | #define _LIBS_CUTILS_THREADS_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /***********************************************************************/ 25 | /***********************************************************************/ 26 | /***** *****/ 27 | /***** local thread storage *****/ 28 | /***** *****/ 29 | /***********************************************************************/ 30 | /***********************************************************************/ 31 | 32 | #ifdef HAVE_PTHREADS 33 | 34 | #include 35 | 36 | typedef struct { 37 | pthread_mutex_t lock; 38 | int has_tls; 39 | pthread_key_t tls; 40 | 41 | } thread_store_t; 42 | 43 | #define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 } 44 | 45 | #elif defined HAVE_WIN32_THREADS 46 | 47 | #include 48 | 49 | typedef struct { 50 | int lock_init; 51 | int has_tls; 52 | DWORD tls; 53 | CRITICAL_SECTION lock; 54 | 55 | } thread_store_t; 56 | 57 | #define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} } 58 | 59 | #else 60 | # error "no thread_store_t implementation for your platform !!" 61 | #endif 62 | 63 | typedef void (*thread_store_destruct_t)(void* value); 64 | 65 | extern void* thread_store_get(thread_store_t* store); 66 | 67 | extern void thread_store_set(thread_store_t* store, 68 | void* value, 69 | thread_store_destruct_t destroy); 70 | 71 | /***********************************************************************/ 72 | /***********************************************************************/ 73 | /***** *****/ 74 | /***** mutexes *****/ 75 | /***** *****/ 76 | /***********************************************************************/ 77 | /***********************************************************************/ 78 | 79 | #ifdef HAVE_PTHREADS 80 | 81 | typedef pthread_mutex_t mutex_t; 82 | 83 | #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 84 | 85 | static __inline__ void mutex_lock(mutex_t* lock) 86 | { 87 | pthread_mutex_lock(lock); 88 | } 89 | static __inline__ void mutex_unlock(mutex_t* lock) 90 | { 91 | pthread_mutex_unlock(lock); 92 | } 93 | static __inline__ int mutex_init(mutex_t* lock) 94 | { 95 | return pthread_mutex_init(lock, NULL); 96 | } 97 | static __inline__ void mutex_destroy(mutex_t* lock) 98 | { 99 | pthread_mutex_destroy(lock); 100 | } 101 | #endif 102 | 103 | #ifdef HAVE_WIN32_THREADS 104 | typedef struct { 105 | int init; 106 | CRITICAL_SECTION lock[1]; 107 | } mutex_t; 108 | 109 | #define MUTEX_INITIALIZER { 0, {{ NULL, 0, 0, NULL, NULL, 0 }} } 110 | 111 | static __inline__ void mutex_lock(mutex_t* lock) 112 | { 113 | if (!lock->init) { 114 | lock->init = 1; 115 | InitializeCriticalSection( lock->lock ); 116 | lock->init = 2; 117 | } else while (lock->init != 2) 118 | Sleep(10); 119 | 120 | EnterCriticalSection(lock->lock); 121 | } 122 | 123 | static __inline__ void mutex_unlock(mutex_t* lock) 124 | { 125 | LeaveCriticalSection(lock->lock); 126 | } 127 | static __inline__ int mutex_init(mutex_t* lock) 128 | { 129 | InitializeCriticalSection(lock->lock); 130 | lock->init = 2; 131 | return 0; 132 | } 133 | static __inline__ void mutex_destroy(mutex_t* lock) 134 | { 135 | if (lock->init) { 136 | lock->init = 0; 137 | DeleteCriticalSection(lock->lock); 138 | } 139 | } 140 | #endif 141 | 142 | #ifdef __cplusplus 143 | } 144 | #endif 145 | 146 | #endif /* _LIBS_CUTILS_THREADS_H */ 147 | -------------------------------------------------------------------------------- /libs/include/cutils/uio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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 | // 18 | // implementation of sys/uio.h for platforms that don't have it (Win32) 19 | // 20 | #ifndef _LIBS_CUTILS_UIO_H 21 | #define _LIBS_CUTILS_UIO_H 22 | 23 | #ifdef HAVE_SYS_UIO_H 24 | #include 25 | #else 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | 33 | struct iovec { 34 | const void* iov_base; 35 | size_t iov_len; 36 | }; 37 | 38 | extern int readv( int fd, struct iovec* vecs, int count ); 39 | extern int writev( int fd, const struct iovec* vecs, int count ); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* !HAVE_SYS_UIO_H */ 46 | 47 | #endif /* _LIBS_UTILS_UIO_H */ 48 | 49 | -------------------------------------------------------------------------------- /libs/include/private/binder/Static.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 | // All static variables go here, to control initialization and 18 | // destruction order in the library. 19 | 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | namespace android { 29 | 30 | // For ProcessState.cpp 31 | extern Mutex gProcessMutex; 32 | extern sp gProcess; 33 | 34 | // For ServiceManager.cpp 35 | extern Mutex gDefaultServiceManagerLock; 36 | extern sp gDefaultServiceManager; 37 | extern sp gPermissionController; 38 | 39 | } // namespace android 40 | -------------------------------------------------------------------------------- /libs/include/private/binder/binder_module.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 _BINDER_MODULE_H_ 18 | #define _BINDER_MODULE_H_ 19 | 20 | #ifdef __cplusplus 21 | namespace android { 22 | #endif 23 | 24 | /* obtain structures and constants from the kernel header */ 25 | 26 | #include 27 | #include 28 | 29 | #ifdef __cplusplus 30 | } // namespace android 31 | #endif 32 | 33 | #endif // _BINDER_MODULE_H_ 34 | -------------------------------------------------------------------------------- /libs/include/private/utils/Static.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 | // All static variables go here, to control initialization and 18 | // destruction order in the library. 19 | 20 | #include 21 | #include 22 | 23 | namespace android { 24 | // For TextStream.cpp 25 | extern Vector gTextBuffers; 26 | 27 | // For String8.cpp 28 | extern void initialize_string8(); 29 | extern void terminate_string8(); 30 | 31 | // For String16.cpp 32 | extern void initialize_string16(); 33 | extern void terminate_string16(); 34 | 35 | } // namespace android 36 | -------------------------------------------------------------------------------- /libs/include/utils/Atomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_UTILS_ATOMIC_H 18 | #define ANDROID_UTILS_ATOMIC_H 19 | 20 | #include 21 | 22 | #endif // ANDROID_UTILS_ATOMIC_H 23 | -------------------------------------------------------------------------------- /libs/include/utils/BufferedTextOutput.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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_BUFFEREDTEXTOUTPUT_H 18 | #define ANDROID_BUFFEREDTEXTOUTPUT_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | // --------------------------------------------------------------------------- 25 | namespace android { 26 | 27 | class BufferedTextOutput : public TextOutput 28 | { 29 | public: 30 | //** Flags for constructor */ 31 | enum { 32 | MULTITHREADED = 0x0001 33 | }; 34 | 35 | BufferedTextOutput(uint32_t flags = 0); 36 | virtual ~BufferedTextOutput(); 37 | 38 | virtual status_t print(const char* txt, size_t len); 39 | virtual void moveIndent(int delta); 40 | 41 | virtual void pushBundle(); 42 | virtual void popBundle(); 43 | 44 | protected: 45 | virtual status_t writeLines(const struct iovec& vec, size_t N) = 0; 46 | 47 | private: 48 | struct BufferState; 49 | struct ThreadState; 50 | 51 | static ThreadState*getThreadState(); 52 | static void threadDestructor(void *st); 53 | 54 | BufferState*getBuffer() const; 55 | 56 | uint32_t mFlags; 57 | const int32_t mSeq; 58 | const int32_t mIndex; 59 | 60 | Mutex mLock; 61 | BufferState* mGlobalState; 62 | }; 63 | 64 | // --------------------------------------------------------------------------- 65 | }; // namespace android 66 | 67 | #endif // ANDROID_BUFFEREDTEXTOUTPUT_H 68 | -------------------------------------------------------------------------------- /libs/include/utils/CallStack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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_CALLSTACK_H 18 | #define ANDROID_CALLSTACK_H 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | // --------------------------------------------------------------------------- 26 | 27 | namespace android { 28 | 29 | class CallStack 30 | { 31 | public: 32 | enum { 33 | MAX_DEPTH = 31 34 | }; 35 | 36 | CallStack(); 37 | CallStack(const CallStack& rhs); 38 | ~CallStack(); 39 | 40 | CallStack& operator = (const CallStack& rhs); 41 | 42 | bool operator == (const CallStack& rhs) const; 43 | bool operator != (const CallStack& rhs) const; 44 | bool operator < (const CallStack& rhs) const; 45 | bool operator >= (const CallStack& rhs) const; 46 | bool operator > (const CallStack& rhs) const; 47 | bool operator <= (const CallStack& rhs) const; 48 | 49 | const void* operator [] (int index) const; 50 | 51 | void clear(); 52 | 53 | void update(int32_t ignoreDepth=1, int32_t maxDepth=MAX_DEPTH); 54 | 55 | // Dump a stack trace to the log 56 | void dump(const char* prefix = 0) const; 57 | 58 | // Return a string (possibly very long) containing the complete stack trace 59 | String8 toString(const char* prefix = 0) const; 60 | 61 | size_t size() const { return mCount; } 62 | 63 | private: 64 | // Internal helper function 65 | String8 toStringSingleLevel(const char* prefix, int32_t level) const; 66 | 67 | size_t mCount; 68 | const void* mStack[MAX_DEPTH]; 69 | }; 70 | 71 | }; // namespace android 72 | 73 | 74 | // --------------------------------------------------------------------------- 75 | 76 | #endif // ANDROID_CALLSTACK_H 77 | -------------------------------------------------------------------------------- /libs/include/utils/Debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_DEBUG_H 18 | #define ANDROID_DEBUG_H 19 | 20 | #include 21 | #include 22 | 23 | namespace android { 24 | // --------------------------------------------------------------------------- 25 | 26 | #ifdef __cplusplus 27 | template struct CompileTimeAssert; 28 | template<> struct CompileTimeAssert {}; 29 | #define COMPILE_TIME_ASSERT(_exp) \ 30 | template class CompileTimeAssert< (_exp) >; 31 | #endif 32 | #define COMPILE_TIME_ASSERT_FUNCTION_SCOPE(_exp) \ 33 | CompileTimeAssert<( _exp )>(); 34 | 35 | // --------------------------------------------------------------------------- 36 | 37 | #ifdef __cplusplus 38 | template struct CompileTimeIfElse; 39 | template 40 | struct CompileTimeIfElse { typedef LHS TYPE; }; 41 | template 42 | struct CompileTimeIfElse { typedef RHS TYPE; }; 43 | #endif 44 | 45 | // --------------------------------------------------------------------------- 46 | 47 | #ifdef __cplusplus 48 | extern "C" { 49 | #endif 50 | 51 | const char* stringForIndent(int32_t indentLevel); 52 | 53 | typedef void (*debugPrintFunc)(void* cookie, const char* txt); 54 | 55 | void printTypeCode(uint32_t typeCode, 56 | debugPrintFunc func = 0, void* cookie = 0); 57 | 58 | void printHexData(int32_t indent, const void *buf, size_t length, 59 | size_t bytesPerLine=16, int32_t singleLineBytesCutoff=16, 60 | size_t alignment=0, bool cArrayStyle=false, 61 | debugPrintFunc func = 0, void* cookie = 0); 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | 67 | // --------------------------------------------------------------------------- 68 | }; // namespace android 69 | 70 | #endif // ANDROID_DEBUG_H 71 | -------------------------------------------------------------------------------- /libs/include/utils/Endian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // 18 | // Android endian-ness defines. 19 | // 20 | #ifndef _LIBS_UTILS_ENDIAN_H 21 | #define _LIBS_UTILS_ENDIAN_H 22 | 23 | #if defined(HAVE_ENDIAN_H) 24 | 25 | #include 26 | 27 | #else /*not HAVE_ENDIAN_H*/ 28 | 29 | #define __BIG_ENDIAN 0x1000 30 | #define __LITTLE_ENDIAN 0x0001 31 | 32 | #if defined(HAVE_LITTLE_ENDIAN) 33 | # define __BYTE_ORDER __LITTLE_ENDIAN 34 | #else 35 | # define __BYTE_ORDER __BIG_ENDIAN 36 | #endif 37 | 38 | #endif /*not HAVE_ENDIAN_H*/ 39 | 40 | #endif /*_LIBS_UTILS_ENDIAN_H*/ 41 | -------------------------------------------------------------------------------- /libs/include/utils/Errors.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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_ERRORS_H 18 | #define ANDROID_ERRORS_H 19 | 20 | #include 21 | #include 22 | 23 | namespace android { 24 | 25 | // use this type to return error codes 26 | #ifdef HAVE_MS_C_RUNTIME 27 | typedef int status_t; 28 | #else 29 | typedef int32_t status_t; 30 | #endif 31 | 32 | /* the MS C runtime lacks a few error codes */ 33 | 34 | /* 35 | * Error codes. 36 | * All error codes are negative values. 37 | */ 38 | 39 | // Win32 #defines NO_ERROR as well. It has the same value, so there's no 40 | // real conflict, though it's a bit awkward. 41 | #ifdef _WIN32 42 | # undef NO_ERROR 43 | #endif 44 | 45 | enum { 46 | OK = 0, // Everything's swell. 47 | NO_ERROR = 0, // No errors. 48 | 49 | UNKNOWN_ERROR = 0x80000000, 50 | 51 | NO_MEMORY = -ENOMEM, 52 | INVALID_OPERATION = -ENOSYS, 53 | BAD_VALUE = -EINVAL, 54 | BAD_TYPE = 0x80000001, 55 | NAME_NOT_FOUND = -ENOENT, 56 | PERMISSION_DENIED = -EPERM, 57 | NO_INIT = -ENODEV, 58 | ALREADY_EXISTS = -EEXIST, 59 | DEAD_OBJECT = -EPIPE, 60 | FAILED_TRANSACTION = 0x80000002, 61 | JPARKS_BROKE_IT = -EPIPE, 62 | #if !defined(HAVE_MS_C_RUNTIME) 63 | BAD_INDEX = -EOVERFLOW, 64 | NOT_ENOUGH_DATA = -ENODATA, 65 | WOULD_BLOCK = -EWOULDBLOCK, 66 | TIMED_OUT = -ETIMEDOUT, 67 | UNKNOWN_TRANSACTION = -EBADMSG, 68 | #else 69 | BAD_INDEX = -E2BIG, 70 | NOT_ENOUGH_DATA = 0x80000003, 71 | WOULD_BLOCK = 0x80000004, 72 | TIMED_OUT = 0x80000005, 73 | UNKNOWN_TRANSACTION = 0x80000006, 74 | #endif 75 | FDS_NOT_ALLOWED = 0x80000007, 76 | }; 77 | 78 | // Restore define; enumeration is in "android" namespace, so the value defined 79 | // there won't work for Win32 code in a different namespace. 80 | #ifdef _WIN32 81 | # define NO_ERROR 0L 82 | #endif 83 | 84 | }; // namespace android 85 | 86 | // --------------------------------------------------------------------------- 87 | 88 | #endif // ANDROID_ERRORS_H 89 | -------------------------------------------------------------------------------- /libs/include/utils/Flattenable.h: -------------------------------------------------------------------------------- 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 | #ifndef ANDROID_UTILS_FLATTENABLE_H 18 | #define ANDROID_UTILS_FLATTENABLE_H 19 | 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | namespace android { 26 | 27 | class Flattenable 28 | { 29 | public: 30 | // size in bytes of the flattened object 31 | virtual size_t getFlattenedSize() const = 0; 32 | 33 | // number of file descriptors to flatten 34 | virtual size_t getFdCount() const = 0; 35 | 36 | // flattens the object into buffer. 37 | // size should be at least of getFlattenedSize() 38 | // file descriptors are written in the fds[] array but ownership is 39 | // not transfered (ie: they must be dupped by the caller of 40 | // flatten() if needed). 41 | virtual status_t flatten(void* buffer, size_t size, 42 | int fds[], size_t count) const = 0; 43 | 44 | // unflattens the object from buffer. 45 | // size should be equal to the value of getFlattenedSize() when the 46 | // object was flattened. 47 | // unflattened file descriptors are found in the fds[] array and 48 | // don't need to be dupped(). ie: the caller of unflatten doesn't 49 | // keep ownership. If a fd is not retained by unflatten() it must be 50 | // explicitly closed. 51 | virtual status_t unflatten(void const* buffer, size_t size, 52 | int fds[], size_t count) = 0; 53 | 54 | protected: 55 | virtual ~Flattenable() = 0; 56 | 57 | }; 58 | 59 | }; // namespace android 60 | 61 | 62 | #endif /* ANDROID_UTILS_FLATTENABLE_H */ 63 | -------------------------------------------------------------------------------- /libs/include/utils/Log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // 18 | // C/C++ logging functions. See the logging documentation for API details. 19 | // 20 | // We'd like these to be available from C code (in case we import some from 21 | // somewhere), so this has a C interface. 22 | // 23 | // The output will be correct when the log file is shared between multiple 24 | // threads and/or multiple processes so long as the operating system 25 | // supports O_APPEND. These calls have mutex-protected data structures 26 | // and so are NOT reentrant. Do not use LOG in a signal handler. 27 | // 28 | #ifndef _LIBS_UTILS_LOG_H 29 | #define _LIBS_UTILS_LOG_H 30 | 31 | #include 32 | 33 | #endif // _LIBS_UTILS_LOG_H 34 | -------------------------------------------------------------------------------- /libs/include/utils/SharedBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_SHARED_BUFFER_H 18 | #define ANDROID_SHARED_BUFFER_H 19 | 20 | #include 21 | #include 22 | 23 | // --------------------------------------------------------------------------- 24 | 25 | namespace android { 26 | 27 | class SharedBuffer 28 | { 29 | public: 30 | 31 | /* flags to use with release() */ 32 | enum { 33 | eKeepStorage = 0x00000001 34 | }; 35 | 36 | /*! allocate a buffer of size 'size' and acquire() it. 37 | * call release() to free it. 38 | */ 39 | static SharedBuffer* alloc(size_t size); 40 | 41 | /*! free the memory associated with the SharedBuffer. 42 | * Fails if there are any users associated with this SharedBuffer. 43 | * In other words, the buffer must have been release by all its 44 | * users. 45 | */ 46 | static ssize_t dealloc(const SharedBuffer* released); 47 | 48 | //! get the SharedBuffer from the data pointer 49 | static inline const SharedBuffer* sharedBuffer(const void* data); 50 | 51 | //! access the data for read 52 | inline const void* data() const; 53 | 54 | //! access the data for read/write 55 | inline void* data(); 56 | 57 | //! get size of the buffer 58 | inline size_t size() const; 59 | 60 | //! get back a SharedBuffer object from its data 61 | static inline SharedBuffer* bufferFromData(void* data); 62 | 63 | //! get back a SharedBuffer object from its data 64 | static inline const SharedBuffer* bufferFromData(const void* data); 65 | 66 | //! get the size of a SharedBuffer object from its data 67 | static inline size_t sizeFromData(const void* data); 68 | 69 | //! edit the buffer (get a writtable, or non-const, version of it) 70 | SharedBuffer* edit() const; 71 | 72 | //! edit the buffer, resizing if needed 73 | SharedBuffer* editResize(size_t size) const; 74 | 75 | //! like edit() but fails if a copy is required 76 | SharedBuffer* attemptEdit() const; 77 | 78 | //! resize and edit the buffer, loose it's content. 79 | SharedBuffer* reset(size_t size) const; 80 | 81 | //! acquire/release a reference on this buffer 82 | void acquire() const; 83 | 84 | /*! release a reference on this buffer, with the option of not 85 | * freeing the memory associated with it if it was the last reference 86 | * returns the previous reference count 87 | */ 88 | int32_t release(uint32_t flags = 0) const; 89 | 90 | //! returns wether or not we're the only owner 91 | inline bool onlyOwner() const; 92 | 93 | 94 | private: 95 | inline SharedBuffer() { } 96 | inline ~SharedBuffer() { } 97 | inline SharedBuffer(const SharedBuffer&); 98 | 99 | // 16 bytes. must be sized to preserve correct alingment. 100 | mutable int32_t mRefs; 101 | size_t mSize; 102 | uint32_t mReserved[2]; 103 | }; 104 | 105 | // --------------------------------------------------------------------------- 106 | 107 | const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) { 108 | return data ? reinterpret_cast(data)-1 : 0; 109 | } 110 | 111 | const void* SharedBuffer::data() const { 112 | return this + 1; 113 | } 114 | 115 | void* SharedBuffer::data() { 116 | return this + 1; 117 | } 118 | 119 | size_t SharedBuffer::size() const { 120 | return mSize; 121 | } 122 | 123 | SharedBuffer* SharedBuffer::bufferFromData(void* data) 124 | { 125 | return ((SharedBuffer*)data)-1; 126 | } 127 | 128 | const SharedBuffer* SharedBuffer::bufferFromData(const void* data) 129 | { 130 | return ((const SharedBuffer*)data)-1; 131 | } 132 | 133 | size_t SharedBuffer::sizeFromData(const void* data) 134 | { 135 | return (((const SharedBuffer*)data)-1)->mSize; 136 | } 137 | 138 | bool SharedBuffer::onlyOwner() const { 139 | return (mRefs == 1); 140 | } 141 | 142 | }; // namespace android 143 | 144 | // --------------------------------------------------------------------------- 145 | 146 | #endif // ANDROID_VECTOR_H 147 | -------------------------------------------------------------------------------- /libs/include/utils/Singleton.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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_UTILS_SINGLETON_H 18 | #define ANDROID_UTILS_SINGLETON_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | namespace android { 26 | // --------------------------------------------------------------------------- 27 | 28 | template 29 | class ANDROID_API Singleton 30 | { 31 | public: 32 | static TYPE& getInstance() { 33 | Mutex::Autolock _l(sLock); 34 | TYPE* instance = sInstance; 35 | if (instance == 0) { 36 | instance = new TYPE(); 37 | sInstance = instance; 38 | } 39 | return *instance; 40 | } 41 | 42 | static bool hasInstance() { 43 | Mutex::Autolock _l(sLock); 44 | return sInstance != 0; 45 | } 46 | 47 | protected: 48 | ~Singleton() { }; 49 | Singleton() { }; 50 | 51 | private: 52 | Singleton(const Singleton&); 53 | Singleton& operator = (const Singleton&); 54 | static Mutex sLock; 55 | static TYPE* sInstance; 56 | }; 57 | 58 | /* 59 | * use ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) in your implementation file 60 | * (eg: .cpp) to create the static instance of Singleton<>'s attributes, 61 | * and avoid to have a copy of them in each compilation units Singleton 62 | * is used. 63 | * NOTE: we use a version of Mutex ctor that takes a parameter, because 64 | * for some unknown reason using the default ctor doesn't emit the variable! 65 | */ 66 | 67 | #define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) \ 68 | template class Singleton< TYPE >; \ 69 | template<> Mutex Singleton< TYPE >::sLock(Mutex::PRIVATE); \ 70 | template<> TYPE* Singleton< TYPE >::sInstance(0); 71 | 72 | 73 | // --------------------------------------------------------------------------- 74 | }; // namespace android 75 | 76 | #endif // ANDROID_UTILS_SINGLETON_H 77 | 78 | -------------------------------------------------------------------------------- /libs/include/utils/StrongPointer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_STRONG_POINTER_H 18 | #define ANDROID_STRONG_POINTER_H 19 | 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | // --------------------------------------------------------------------------- 27 | namespace android { 28 | 29 | class TextOutput; 30 | TextOutput& printStrongPointer(TextOutput& to, const void* val); 31 | 32 | template class wp; 33 | 34 | // --------------------------------------------------------------------------- 35 | 36 | #define COMPARE(_op_) \ 37 | inline bool operator _op_ (const sp& o) const { \ 38 | return m_ptr _op_ o.m_ptr; \ 39 | } \ 40 | inline bool operator _op_ (const T* o) const { \ 41 | return m_ptr _op_ o; \ 42 | } \ 43 | template \ 44 | inline bool operator _op_ (const sp& o) const { \ 45 | return m_ptr _op_ o.m_ptr; \ 46 | } \ 47 | template \ 48 | inline bool operator _op_ (const U* o) const { \ 49 | return m_ptr _op_ o; \ 50 | } \ 51 | inline bool operator _op_ (const wp& o) const { \ 52 | return m_ptr _op_ o.m_ptr; \ 53 | } \ 54 | template \ 55 | inline bool operator _op_ (const wp& o) const { \ 56 | return m_ptr _op_ o.m_ptr; \ 57 | } 58 | 59 | // --------------------------------------------------------------------------- 60 | 61 | template 62 | class sp 63 | { 64 | public: 65 | inline sp() : m_ptr(0) { } 66 | 67 | sp(T* other); 68 | sp(const sp& other); 69 | template sp(U* other); 70 | template sp(const sp& other); 71 | 72 | ~sp(); 73 | 74 | // Assignment 75 | 76 | sp& operator = (T* other); 77 | sp& operator = (const sp& other); 78 | 79 | template sp& operator = (const sp& other); 80 | template sp& operator = (U* other); 81 | 82 | //! Special optimization for use by ProcessState (and nobody else). 83 | void force_set(T* other); 84 | 85 | // Reset 86 | 87 | void clear(); 88 | 89 | // Accessors 90 | 91 | inline T& operator* () const { return *m_ptr; } 92 | inline T* operator-> () const { return m_ptr; } 93 | inline T* get() const { return m_ptr; } 94 | 95 | // Operators 96 | 97 | COMPARE(==) 98 | COMPARE(!=) 99 | COMPARE(>) 100 | COMPARE(<) 101 | COMPARE(<=) 102 | COMPARE(>=) 103 | 104 | private: 105 | template friend class sp; 106 | template friend class wp; 107 | void set_pointer(T* ptr); 108 | T* m_ptr; 109 | }; 110 | 111 | #undef COMPARE 112 | 113 | template 114 | TextOutput& operator<<(TextOutput& to, const sp& val); 115 | 116 | // --------------------------------------------------------------------------- 117 | // No user serviceable parts below here. 118 | 119 | template 120 | sp::sp(T* other) 121 | : m_ptr(other) 122 | { 123 | if (other) other->incStrong(this); 124 | } 125 | 126 | template 127 | sp::sp(const sp& other) 128 | : m_ptr(other.m_ptr) 129 | { 130 | if (m_ptr) m_ptr->incStrong(this); 131 | } 132 | 133 | template template 134 | sp::sp(U* other) : m_ptr(other) 135 | { 136 | if (other) ((T*)other)->incStrong(this); 137 | } 138 | 139 | template template 140 | sp::sp(const sp& other) 141 | : m_ptr(other.m_ptr) 142 | { 143 | if (m_ptr) m_ptr->incStrong(this); 144 | } 145 | 146 | template 147 | sp::~sp() 148 | { 149 | if (m_ptr) m_ptr->decStrong(this); 150 | } 151 | 152 | template 153 | sp& sp::operator = (const sp& other) { 154 | T* otherPtr(other.m_ptr); 155 | if (otherPtr) otherPtr->incStrong(this); 156 | if (m_ptr) m_ptr->decStrong(this); 157 | m_ptr = otherPtr; 158 | return *this; 159 | } 160 | 161 | template 162 | sp& sp::operator = (T* other) 163 | { 164 | if (other) other->incStrong(this); 165 | if (m_ptr) m_ptr->decStrong(this); 166 | m_ptr = other; 167 | return *this; 168 | } 169 | 170 | template template 171 | sp& sp::operator = (const sp& other) 172 | { 173 | T* otherPtr(other.m_ptr); 174 | if (otherPtr) otherPtr->incStrong(this); 175 | if (m_ptr) m_ptr->decStrong(this); 176 | m_ptr = otherPtr; 177 | return *this; 178 | } 179 | 180 | template template 181 | sp& sp::operator = (U* other) 182 | { 183 | if (other) ((T*)other)->incStrong(this); 184 | if (m_ptr) m_ptr->decStrong(this); 185 | m_ptr = other; 186 | return *this; 187 | } 188 | 189 | template 190 | void sp::force_set(T* other) 191 | { 192 | other->forceIncStrong(this); 193 | m_ptr = other; 194 | } 195 | 196 | template 197 | void sp::clear() 198 | { 199 | if (m_ptr) { 200 | m_ptr->decStrong(this); 201 | m_ptr = 0; 202 | } 203 | } 204 | 205 | template 206 | void sp::set_pointer(T* ptr) { 207 | m_ptr = ptr; 208 | } 209 | 210 | template 211 | inline TextOutput& operator<<(TextOutput& to, const sp& val) 212 | { 213 | return printStrongPointer(to, val.get()); 214 | } 215 | 216 | }; // namespace android 217 | 218 | // --------------------------------------------------------------------------- 219 | 220 | #endif // ANDROID_STRONG_POINTER_H 221 | -------------------------------------------------------------------------------- /libs/include/utils/SystemClock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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_UTILS_SYSTEMCLOCK_H 18 | #define ANDROID_UTILS_SYSTEMCLOCK_H 19 | 20 | #include 21 | #include 22 | 23 | namespace android { 24 | 25 | int setCurrentTimeMillis(int64_t millis); 26 | int64_t uptimeMillis(); 27 | int64_t elapsedRealtime(); 28 | 29 | }; // namespace android 30 | 31 | #endif // ANDROID_UTILS_SYSTEMCLOCK_H 32 | 33 | -------------------------------------------------------------------------------- /libs/include/utils/TextOutput.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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_TEXTOUTPUT_H 18 | #define ANDROID_TEXTOUTPUT_H 19 | 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | // --------------------------------------------------------------------------- 26 | namespace android { 27 | 28 | class TextOutput 29 | { 30 | public: 31 | TextOutput(); 32 | virtual ~TextOutput(); 33 | 34 | virtual status_t print(const char* txt, size_t len) = 0; 35 | virtual void moveIndent(int delta) = 0; 36 | 37 | class Bundle { 38 | public: 39 | inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); } 40 | inline ~Bundle() { mTO.popBundle(); } 41 | private: 42 | TextOutput& mTO; 43 | }; 44 | 45 | virtual void pushBundle() = 0; 46 | virtual void popBundle() = 0; 47 | }; 48 | 49 | // --------------------------------------------------------------------------- 50 | 51 | // Text output stream for printing to the log (via utils/Log.h). 52 | extern TextOutput& alog; 53 | 54 | // Text output stream for printing to stdout. 55 | extern TextOutput& aout; 56 | 57 | // Text output stream for printing to stderr. 58 | extern TextOutput& aerr; 59 | 60 | typedef TextOutput& (*TextOutputManipFunc)(TextOutput&); 61 | 62 | TextOutput& endl(TextOutput& to); 63 | TextOutput& indent(TextOutput& to); 64 | TextOutput& dedent(TextOutput& to); 65 | 66 | TextOutput& operator<<(TextOutput& to, const char* str); 67 | TextOutput& operator<<(TextOutput& to, char); // writes raw character 68 | TextOutput& operator<<(TextOutput& to, bool); 69 | TextOutput& operator<<(TextOutput& to, int); 70 | TextOutput& operator<<(TextOutput& to, long); 71 | TextOutput& operator<<(TextOutput& to, unsigned int); 72 | TextOutput& operator<<(TextOutput& to, unsigned long); 73 | TextOutput& operator<<(TextOutput& to, long long); 74 | TextOutput& operator<<(TextOutput& to, unsigned long long); 75 | TextOutput& operator<<(TextOutput& to, float); 76 | TextOutput& operator<<(TextOutput& to, double); 77 | TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func); 78 | TextOutput& operator<<(TextOutput& to, const void*); 79 | 80 | class TypeCode 81 | { 82 | public: 83 | inline TypeCode(uint32_t code); 84 | inline ~TypeCode(); 85 | 86 | inline uint32_t typeCode() const; 87 | 88 | private: 89 | uint32_t mCode; 90 | }; 91 | 92 | TextOutput& operator<<(TextOutput& to, const TypeCode& val); 93 | 94 | class HexDump 95 | { 96 | public: 97 | HexDump(const void *buf, size_t size, size_t bytesPerLine=16); 98 | inline ~HexDump(); 99 | 100 | inline HexDump& setBytesPerLine(size_t bytesPerLine); 101 | inline HexDump& setSingleLineCutoff(int32_t bytes); 102 | inline HexDump& setAlignment(size_t alignment); 103 | inline HexDump& setCArrayStyle(bool enabled); 104 | 105 | inline const void* buffer() const; 106 | inline size_t size() const; 107 | inline size_t bytesPerLine() const; 108 | inline int32_t singleLineCutoff() const; 109 | inline size_t alignment() const; 110 | inline bool carrayStyle() const; 111 | 112 | private: 113 | const void* mBuffer; 114 | size_t mSize; 115 | size_t mBytesPerLine; 116 | int32_t mSingleLineCutoff; 117 | size_t mAlignment; 118 | bool mCArrayStyle; 119 | }; 120 | 121 | TextOutput& operator<<(TextOutput& to, const HexDump& val); 122 | 123 | // --------------------------------------------------------------------------- 124 | // No user servicable parts below. 125 | 126 | inline TextOutput& endl(TextOutput& to) 127 | { 128 | to.print("\n", 1); 129 | return to; 130 | } 131 | 132 | inline TextOutput& indent(TextOutput& to) 133 | { 134 | to.moveIndent(1); 135 | return to; 136 | } 137 | 138 | inline TextOutput& dedent(TextOutput& to) 139 | { 140 | to.moveIndent(-1); 141 | return to; 142 | } 143 | 144 | inline TextOutput& operator<<(TextOutput& to, const char* str) 145 | { 146 | to.print(str, strlen(str)); 147 | return to; 148 | } 149 | 150 | inline TextOutput& operator<<(TextOutput& to, char c) 151 | { 152 | to.print(&c, 1); 153 | return to; 154 | } 155 | 156 | inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func) 157 | { 158 | return (*func)(to); 159 | } 160 | 161 | inline TypeCode::TypeCode(uint32_t code) : mCode(code) { } 162 | inline TypeCode::~TypeCode() { } 163 | inline uint32_t TypeCode::typeCode() const { return mCode; } 164 | 165 | inline HexDump::~HexDump() { } 166 | 167 | inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) { 168 | mBytesPerLine = bytesPerLine; return *this; 169 | } 170 | inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) { 171 | mSingleLineCutoff = bytes; return *this; 172 | } 173 | inline HexDump& HexDump::setAlignment(size_t alignment) { 174 | mAlignment = alignment; return *this; 175 | } 176 | inline HexDump& HexDump::setCArrayStyle(bool enabled) { 177 | mCArrayStyle = enabled; return *this; 178 | } 179 | 180 | inline const void* HexDump::buffer() const { return mBuffer; } 181 | inline size_t HexDump::size() const { return mSize; } 182 | inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; } 183 | inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; } 184 | inline size_t HexDump::alignment() const { return mAlignment; } 185 | inline bool HexDump::carrayStyle() const { return mCArrayStyle; } 186 | 187 | // --------------------------------------------------------------------------- 188 | }; // namespace android 189 | 190 | #endif // ANDROID_TEXTOUTPUT_H 191 | -------------------------------------------------------------------------------- /libs/include/utils/Timers.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // 18 | // Timer functions. 19 | // 20 | #ifndef _LIBS_UTILS_TIMERS_H 21 | #define _LIBS_UTILS_TIMERS_H 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | // ------------------------------------------------------------------ 28 | // C API 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | typedef int64_t nsecs_t; // nano-seconds 35 | 36 | static inline nsecs_t seconds_to_nanoseconds(nsecs_t secs) 37 | { 38 | return secs*1000000000; 39 | } 40 | 41 | static inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs) 42 | { 43 | return secs*1000000; 44 | } 45 | 46 | static inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs) 47 | { 48 | return secs*1000; 49 | } 50 | 51 | static inline nsecs_t nanoseconds_to_seconds(nsecs_t secs) 52 | { 53 | return secs/1000000000; 54 | } 55 | 56 | static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs) 57 | { 58 | return secs/1000000; 59 | } 60 | 61 | static inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs) 62 | { 63 | return secs/1000; 64 | } 65 | 66 | static inline nsecs_t s2ns(nsecs_t v) {return seconds_to_nanoseconds(v);} 67 | static inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);} 68 | static inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);} 69 | static inline nsecs_t ns2s(nsecs_t v) {return nanoseconds_to_seconds(v);} 70 | static inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);} 71 | static inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);} 72 | 73 | static inline nsecs_t seconds(nsecs_t v) { return s2ns(v); } 74 | static inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); } 75 | static inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); } 76 | 77 | enum { 78 | SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock 79 | SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point 80 | SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock 81 | SYSTEM_TIME_THREAD = 3 // high-resolution per-thread clock 82 | }; 83 | 84 | // return the system-time according to the specified clock 85 | #ifdef __cplusplus 86 | nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC); 87 | #else 88 | nsecs_t systemTime(int clock); 89 | #endif // def __cplusplus 90 | 91 | /** 92 | * Returns the number of milliseconds to wait between the reference time and the timeout time. 93 | * If the timeout is in the past relative to the reference time, returns 0. 94 | * If the timeout is more than INT_MAX milliseconds in the future relative to the reference time, 95 | * such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay. 96 | * Otherwise, returns the difference between the reference time and timeout time 97 | * rounded up to the next millisecond. 98 | */ 99 | int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime); 100 | 101 | #ifdef __cplusplus 102 | } // extern "C" 103 | #endif 104 | 105 | // ------------------------------------------------------------------ 106 | // C++ API 107 | 108 | #ifdef __cplusplus 109 | 110 | namespace android { 111 | /* 112 | * Time the duration of something. 113 | * 114 | * Includes some timeval manipulation functions. 115 | */ 116 | class DurationTimer { 117 | public: 118 | DurationTimer() {} 119 | ~DurationTimer() {} 120 | 121 | // Start the timer. 122 | void start(); 123 | // Stop the timer. 124 | void stop(); 125 | // Get the duration in microseconds. 126 | long long durationUsecs() const; 127 | 128 | // Subtract two timevals. Returns the difference (ptv1-ptv2) in 129 | // microseconds. 130 | static long long subtractTimevals(const struct timeval* ptv1, 131 | const struct timeval* ptv2); 132 | 133 | // Add the specified amount of time to the timeval. 134 | static void addToTimeval(struct timeval* ptv, long usec); 135 | 136 | private: 137 | struct timeval mStartWhen; 138 | struct timeval mStopWhen; 139 | }; 140 | 141 | }; // android 142 | #endif // def __cplusplus 143 | 144 | #endif // _LIBS_UTILS_TIMERS_H 145 | -------------------------------------------------------------------------------- /libs/include/utils/Unicode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_UNICODE_H 18 | #define ANDROID_UNICODE_H 19 | 20 | #include 21 | #include 22 | 23 | extern "C" { 24 | 25 | typedef uint32_t char32_t; 26 | typedef uint16_t char16_t; 27 | 28 | // Standard string functions on char16_t strings. 29 | int strcmp16(const char16_t *, const char16_t *); 30 | int strncmp16(const char16_t *s1, const char16_t *s2, size_t n); 31 | size_t strlen16(const char16_t *); 32 | size_t strnlen16(const char16_t *, size_t); 33 | char16_t *strcpy16(char16_t *, const char16_t *); 34 | char16_t *strncpy16(char16_t *, const char16_t *, size_t); 35 | 36 | // Version of comparison that supports embedded nulls. 37 | // This is different than strncmp() because we don't stop 38 | // at a nul character and consider the strings to be different 39 | // if the lengths are different (thus we need to supply the 40 | // lengths of both strings). This can also be used when 41 | // your string is not nul-terminated as it will have the 42 | // equivalent result as strcmp16 (unlike strncmp16). 43 | int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2); 44 | 45 | // Version of strzcmp16 for comparing strings in different endianness. 46 | int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2); 47 | 48 | // Standard string functions on char32_t strings. 49 | size_t strlen32(const char32_t *); 50 | size_t strnlen32(const char32_t *, size_t); 51 | 52 | /** 53 | * Measure the length of a UTF-32 string in UTF-8. If the string is invalid 54 | * such as containing a surrogate character, -1 will be returned. 55 | */ 56 | ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len); 57 | 58 | /** 59 | * Stores a UTF-8 string converted from "src" in "dst", if "dst_length" is not 60 | * large enough to store the string, the part of the "src" string is stored 61 | * into "dst" as much as possible. See the examples for more detail. 62 | * Returns the size actually used for storing the string. 63 | * dst" is not null-terminated when dst_len is fully used (like strncpy). 64 | * 65 | * Example 1 66 | * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) 67 | * "src_len" == 2 68 | * "dst_len" >= 7 69 | * -> 70 | * Returned value == 6 71 | * "dst" becomes \xE3\x81\x82\xE3\x81\x84\0 72 | * (note that "dst" is null-terminated) 73 | * 74 | * Example 2 75 | * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) 76 | * "src_len" == 2 77 | * "dst_len" == 5 78 | * -> 79 | * Returned value == 3 80 | * "dst" becomes \xE3\x81\x82\0 81 | * (note that "dst" is null-terminated, but \u3044 is not stored in "dst" 82 | * since "dst" does not have enough size to store the character) 83 | * 84 | * Example 3 85 | * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) 86 | * "src_len" == 2 87 | * "dst_len" == 6 88 | * -> 89 | * Returned value == 6 90 | * "dst" becomes \xE3\x81\x82\xE3\x81\x84 91 | * (note that "dst" is NOT null-terminated, like strncpy) 92 | */ 93 | void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst); 94 | 95 | /** 96 | * Returns the unicode value at "index". 97 | * Returns -1 when the index is invalid (equals to or more than "src_len"). 98 | * If returned value is positive, it is able to be converted to char32_t, which 99 | * is unsigned. Then, if "next_index" is not NULL, the next index to be used is 100 | * stored in "next_index". "next_index" can be NULL. 101 | */ 102 | int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index); 103 | 104 | 105 | /** 106 | * Returns the UTF-8 length of UTF-16 string "src". 107 | */ 108 | ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len); 109 | 110 | /** 111 | * Converts a UTF-16 string to UTF-8. The destination buffer must be large 112 | * enough to fit the UTF-16 as measured by utf16_to_utf8_length with an added 113 | * NULL terminator. 114 | */ 115 | void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst); 116 | 117 | /** 118 | * Returns the length of "src" when "src" is valid UTF-8 string. 119 | * Returns 0 if src is NULL or 0-length string. Returns -1 when the source 120 | * is an invalid string. 121 | * 122 | * This function should be used to determine whether "src" is valid UTF-8 123 | * characters with valid unicode codepoints. "src" must be null-terminated. 124 | * 125 | * If you are going to use other utf8_to_... functions defined in this header 126 | * with string which may not be valid UTF-8 with valid codepoint (form 0 to 127 | * 0x10FFFF), you should use this function before calling others, since the 128 | * other functions do not check whether the string is valid UTF-8 or not. 129 | * 130 | * If you do not care whether "src" is valid UTF-8 or not, you should use 131 | * strlen() as usual, which should be much faster. 132 | */ 133 | ssize_t utf8_length(const char *src); 134 | 135 | /** 136 | * Measure the length of a UTF-32 string. 137 | */ 138 | size_t utf8_to_utf32_length(const char *src, size_t src_len); 139 | 140 | /** 141 | * Stores a UTF-32 string converted from "src" in "dst". "dst" must be large 142 | * enough to store the entire converted string as measured by 143 | * utf8_to_utf32_length plus space for a NULL terminator. 144 | */ 145 | void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst); 146 | 147 | /** 148 | * Returns the UTF-16 length of UTF-8 string "src". 149 | */ 150 | ssize_t utf8_to_utf16_length(const uint8_t* src, size_t srcLen); 151 | 152 | /** 153 | * Convert UTF-8 to UTF-16 including surrogate pairs. 154 | * Returns a pointer to the end of the string (where a null terminator might go 155 | * if you wanted to add one). 156 | */ 157 | char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* src, size_t srcLen, char16_t* dst); 158 | 159 | /** 160 | * Convert UTF-8 to UTF-16 including surrogate pairs. The destination buffer 161 | * must be large enough to hold the result as measured by utf8_to_utf16_length 162 | * plus an added NULL terminator. 163 | */ 164 | void utf8_to_utf16(const uint8_t* src, size_t srcLen, char16_t* dst); 165 | 166 | } 167 | 168 | #endif 169 | -------------------------------------------------------------------------------- /libs/include/utils/misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // 18 | // Handy utility functions and portability code. 19 | // 20 | #ifndef _LIBS_UTILS_MISC_H 21 | #define _LIBS_UTILS_MISC_H 22 | 23 | #include 24 | #include 25 | 26 | namespace android { 27 | 28 | /* get #of elements in a static array */ 29 | #ifndef NELEM 30 | # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) 31 | #endif 32 | 33 | /* 34 | * Make a copy of the string, using "new[]" instead of "malloc". Free the 35 | * string with delete[]. 36 | * 37 | * Returns NULL if "str" is NULL. 38 | */ 39 | char* strdupNew(const char* str); 40 | 41 | /* 42 | * Concatenate an argument vector into a single string. If argc is >= 0 43 | * it will be used; if it's < 0 then the last element in the arg vector 44 | * must be NULL. 45 | * 46 | * This inserts a space between each argument. 47 | * 48 | * This does not automatically add double quotes around arguments with 49 | * spaces in them. This practice is necessary for Win32, because Win32's 50 | * CreateProcess call is stupid. 51 | * 52 | * The caller should delete[] the returned string. 53 | */ 54 | char* concatArgv(int argc, const char* const argv[]); 55 | 56 | /* 57 | * Count up the number of arguments in "argv". The count does not include 58 | * the final NULL entry. 59 | */ 60 | int countArgv(const char* const argv[]); 61 | 62 | /* 63 | * Some utility functions for working with files. These could be made 64 | * part of a "File" class. 65 | */ 66 | typedef enum FileType { 67 | kFileTypeUnknown = 0, 68 | kFileTypeNonexistent, // i.e. ENOENT 69 | kFileTypeRegular, 70 | kFileTypeDirectory, 71 | kFileTypeCharDev, 72 | kFileTypeBlockDev, 73 | kFileTypeFifo, 74 | kFileTypeSymlink, 75 | kFileTypeSocket, 76 | } FileType; 77 | /* get the file's type; follows symlinks */ 78 | FileType getFileType(const char* fileName); 79 | /* get the file's modification date; returns -1 w/errno set on failure */ 80 | time_t getFileModDate(const char* fileName); 81 | 82 | /* 83 | * Round up to the nearest power of 2. Handy for hash tables. 84 | */ 85 | unsigned int roundUpPower2(unsigned int val); 86 | 87 | void strreverse(char* begin, char* end); 88 | void k_itoa(int value, char* str, int base); 89 | char* itoa(int val, int base); 90 | 91 | }; // namespace android 92 | 93 | #endif // _LIBS_UTILS_MISC_H 94 | -------------------------------------------------------------------------------- /libs/utils/Flattenable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 18 | 19 | namespace android { 20 | 21 | Flattenable::~Flattenable() { 22 | } 23 | 24 | }; // namespace android 25 | -------------------------------------------------------------------------------- /libs/utils/Makefile: -------------------------------------------------------------------------------- 1 | utils_sources := \ 2 | CallStack.cpp \ 3 | Debug.cpp \ 4 | Flattenable.cpp \ 5 | misc.cpp \ 6 | RefBase.cpp \ 7 | SharedBuffer.cpp \ 8 | String16.cpp \ 9 | String8.cpp \ 10 | SystemClock.cpp \ 11 | TextOutput.cpp \ 12 | Timers.cpp \ 13 | Unicode.cpp \ 14 | VectorImpl.cpp \ 15 | BufferedTextOutput.cpp \ 16 | Threads.cpp \ 17 | Static.cpp 18 | 19 | objects += $(patsubst %.cpp,utils/%.o, $(utils_sources)) 20 | -------------------------------------------------------------------------------- /libs/utils/SharedBuffer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 18 | #include 19 | 20 | #include 21 | #include 22 | 23 | // --------------------------------------------------------------------------- 24 | 25 | namespace android { 26 | 27 | SharedBuffer* SharedBuffer::alloc(size_t size) 28 | { 29 | SharedBuffer* sb = static_cast(malloc(sizeof(SharedBuffer) + size)); 30 | if (sb) { 31 | sb->mRefs = 1; 32 | sb->mSize = size; 33 | } 34 | return sb; 35 | } 36 | 37 | 38 | ssize_t SharedBuffer::dealloc(const SharedBuffer* released) 39 | { 40 | if (released->mRefs != 0) return -1; // XXX: invalid operation 41 | free(const_cast(released)); 42 | return 0; 43 | } 44 | 45 | SharedBuffer* SharedBuffer::edit() const 46 | { 47 | if (onlyOwner()) { 48 | return const_cast(this); 49 | } 50 | SharedBuffer* sb = alloc(mSize); 51 | if (sb) { 52 | memcpy(sb->data(), data(), size()); 53 | release(); 54 | } 55 | return sb; 56 | } 57 | 58 | SharedBuffer* SharedBuffer::editResize(size_t newSize) const 59 | { 60 | if (onlyOwner()) { 61 | SharedBuffer* buf = const_cast(this); 62 | if (buf->mSize == newSize) return buf; 63 | buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize); 64 | if (buf != NULL) { 65 | buf->mSize = newSize; 66 | return buf; 67 | } 68 | } 69 | SharedBuffer* sb = alloc(newSize); 70 | if (sb) { 71 | const size_t mySize = mSize; 72 | memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize); 73 | release(); 74 | } 75 | return sb; 76 | } 77 | 78 | SharedBuffer* SharedBuffer::attemptEdit() const 79 | { 80 | if (onlyOwner()) { 81 | return const_cast(this); 82 | } 83 | return 0; 84 | } 85 | 86 | SharedBuffer* SharedBuffer::reset(size_t new_size) const 87 | { 88 | // cheap-o-reset. 89 | SharedBuffer* sb = alloc(new_size); 90 | if (sb) { 91 | release(); 92 | } 93 | return sb; 94 | } 95 | 96 | void SharedBuffer::acquire() const { 97 | android_atomic_inc(&mRefs); 98 | } 99 | 100 | int32_t SharedBuffer::release(uint32_t flags) const 101 | { 102 | int32_t prev = 1; 103 | if (onlyOwner() || ((prev = android_atomic_dec(&mRefs)) == 1)) { 104 | mRefs = 0; 105 | if ((flags & eKeepStorage) == 0) { 106 | free(const_cast(this)); 107 | } 108 | } 109 | return prev; 110 | } 111 | 112 | 113 | }; // namespace android 114 | -------------------------------------------------------------------------------- /libs/utils/Static.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 | // All static variables go here, to control initialization and 18 | // destruction order in the library. 19 | 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | namespace android { 26 | 27 | class LibUtilsFirstStatics 28 | { 29 | public: 30 | LibUtilsFirstStatics() 31 | { 32 | initialize_string8(); 33 | initialize_string16(); 34 | } 35 | 36 | ~LibUtilsFirstStatics() 37 | { 38 | terminate_string16(); 39 | terminate_string8(); 40 | } 41 | }; 42 | 43 | static LibUtilsFirstStatics gFirstStatics; 44 | int gDarwinCantLoadAllObjects = 1; 45 | 46 | // ------------ Text output streams 47 | 48 | Vector gTextBuffers; 49 | 50 | class LogTextOutput : public BufferedTextOutput 51 | { 52 | public: 53 | LogTextOutput() : BufferedTextOutput(MULTITHREADED) { } 54 | virtual ~LogTextOutput() { }; 55 | 56 | protected: 57 | virtual status_t writeLines(const struct iovec& vec, size_t N) 58 | { 59 | //android_writevLog(&vec, N); <-- this is now a no-op 60 | if (N != 1) LOGI("WARNING: writeLines N=%d\n", N); 61 | LOGI("%.*s", vec.iov_len, (const char*) vec.iov_base); 62 | return NO_ERROR; 63 | } 64 | }; 65 | 66 | class FdTextOutput : public BufferedTextOutput 67 | { 68 | public: 69 | FdTextOutput(int fd) : BufferedTextOutput(MULTITHREADED), mFD(fd) { } 70 | virtual ~FdTextOutput() { }; 71 | 72 | protected: 73 | virtual status_t writeLines(const struct iovec& vec, size_t N) 74 | { 75 | writev(mFD, &vec, N); 76 | return NO_ERROR; 77 | } 78 | 79 | private: 80 | int mFD; 81 | }; 82 | 83 | static LogTextOutput gLogTextOutput; 84 | static FdTextOutput gStdoutTextOutput(STDOUT_FILENO); 85 | static FdTextOutput gStderrTextOutput(STDERR_FILENO); 86 | 87 | TextOutput& alog(gLogTextOutput); 88 | TextOutput& aout(gStdoutTextOutput); 89 | TextOutput& aerr(gStderrTextOutput); 90 | 91 | } // namespace android 92 | -------------------------------------------------------------------------------- /libs/utils/SystemClock.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 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 | 18 | /* 19 | * System clock functions. 20 | */ 21 | 22 | #ifdef HAVE_ANDROID_OS 23 | #include 24 | #include 25 | #include 26 | #include 27 | #endif 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | #include 37 | 38 | #define LOG_TAG "SystemClock" 39 | #include "utils/Log.h" 40 | 41 | namespace android { 42 | 43 | /* 44 | * Set the current time. This only works when running as root. 45 | */ 46 | int setCurrentTimeMillis(int64_t millis) 47 | { 48 | #if WIN32 49 | // not implemented 50 | return -1; 51 | #else 52 | struct timeval tv; 53 | #ifdef HAVE_ANDROID_OS 54 | struct timespec ts; 55 | int fd; 56 | int res; 57 | #endif 58 | int ret = 0; 59 | 60 | if (millis <= 0 || millis / 1000LL >= INT_MAX) { 61 | return -1; 62 | } 63 | 64 | tv.tv_sec = (time_t) (millis / 1000LL); 65 | tv.tv_usec = (suseconds_t) ((millis % 1000LL) * 1000LL); 66 | 67 | LOGD("Setting time of day to sec=%d\n", (int) tv.tv_sec); 68 | 69 | #ifdef HAVE_ANDROID_OS 70 | fd = open("/dev/alarm", O_RDWR); 71 | if(fd < 0) { 72 | LOGW("Unable to open alarm driver: %s\n", strerror(errno)); 73 | return -1; 74 | } 75 | ts.tv_sec = tv.tv_sec; 76 | ts.tv_nsec = tv.tv_usec * 1000; 77 | res = ioctl(fd, ANDROID_ALARM_SET_RTC, &ts); 78 | if(res < 0) { 79 | LOGW("Unable to set rtc to %ld: %s\n", tv.tv_sec, strerror(errno)); 80 | ret = -1; 81 | } 82 | close(fd); 83 | #else 84 | if (settimeofday(&tv, NULL) != 0) { 85 | LOGW("Unable to set clock to %d.%d: %s\n", 86 | (int) tv.tv_sec, (int) tv.tv_usec, strerror(errno)); 87 | ret = -1; 88 | } 89 | #endif 90 | 91 | return ret; 92 | #endif // WIN32 93 | } 94 | 95 | /* 96 | * native public static long uptimeMillis(); 97 | */ 98 | int64_t uptimeMillis() 99 | { 100 | int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); 101 | return (int64_t) nanoseconds_to_milliseconds(when); 102 | } 103 | 104 | /* 105 | * native public static long elapsedRealtime(); 106 | */ 107 | int64_t elapsedRealtime() 108 | { 109 | #ifdef HAVE_ANDROID_OS 110 | static int s_fd = -1; 111 | 112 | if (s_fd == -1) { 113 | int fd = open("/dev/alarm", O_RDONLY); 114 | if (android_atomic_cmpxchg(-1, fd, &s_fd)) { 115 | close(fd); 116 | } 117 | } 118 | 119 | struct timespec ts; 120 | int result = ioctl(s_fd, 121 | ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts); 122 | 123 | if (result == 0) { 124 | int64_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; 125 | return (int64_t) nanoseconds_to_milliseconds(when); 126 | } else { 127 | // XXX: there was an error, probably because the driver didn't 128 | // exist ... this should return 129 | // a real error, like an exception! 130 | int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); 131 | return (int64_t) nanoseconds_to_milliseconds(when); 132 | } 133 | #else 134 | int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); 135 | return (int64_t) nanoseconds_to_milliseconds(when); 136 | #endif 137 | } 138 | 139 | }; // namespace android 140 | -------------------------------------------------------------------------------- /libs/utils/TextOutput.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 18 | 19 | #include 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | namespace android { 26 | 27 | // --------------------------------------------------------------------------- 28 | 29 | TextOutput::TextOutput() { 30 | } 31 | 32 | TextOutput::~TextOutput() { 33 | } 34 | 35 | // --------------------------------------------------------------------------- 36 | 37 | TextOutput& operator<<(TextOutput& to, bool val) 38 | { 39 | if (val) to.print("true", 4); 40 | else to.print("false", 5); 41 | return to; 42 | } 43 | 44 | TextOutput& operator<<(TextOutput& to, int val) 45 | { 46 | char buf[16]; 47 | sprintf(buf, "%d", val); 48 | to.print(buf, strlen(buf)); 49 | return to; 50 | } 51 | 52 | TextOutput& operator<<(TextOutput& to, long val) 53 | { 54 | char buf[16]; 55 | sprintf(buf, "%ld", val); 56 | to.print(buf, strlen(buf)); 57 | return to; 58 | } 59 | 60 | TextOutput& operator<<(TextOutput& to, unsigned int val) 61 | { 62 | char buf[16]; 63 | sprintf(buf, "%u", val); 64 | to.print(buf, strlen(buf)); 65 | return to; 66 | } 67 | 68 | TextOutput& operator<<(TextOutput& to, unsigned long val) 69 | { 70 | char buf[16]; 71 | sprintf(buf, "%lu", val); 72 | to.print(buf, strlen(buf)); 73 | return to; 74 | } 75 | 76 | TextOutput& operator<<(TextOutput& to, long long val) 77 | { 78 | char buf[32]; 79 | sprintf(buf, "%Ld", val); 80 | to.print(buf, strlen(buf)); 81 | return to; 82 | } 83 | 84 | TextOutput& operator<<(TextOutput& to, unsigned long long val) 85 | { 86 | char buf[32]; 87 | sprintf(buf, "%Lu", val); 88 | to.print(buf, strlen(buf)); 89 | return to; 90 | } 91 | 92 | static TextOutput& print_float(TextOutput& to, double value) 93 | { 94 | char buf[64]; 95 | sprintf(buf, "%g", value); 96 | if( !strchr(buf, '.') && !strchr(buf, 'e') && 97 | !strchr(buf, 'E') ) { 98 | strncat(buf, ".0", sizeof(buf)-1); 99 | } 100 | to.print(buf, strlen(buf)); 101 | return to; 102 | } 103 | 104 | TextOutput& operator<<(TextOutput& to, float val) 105 | { 106 | return print_float(to,val); 107 | } 108 | 109 | TextOutput& operator<<(TextOutput& to, double val) 110 | { 111 | return print_float(to,val); 112 | } 113 | 114 | TextOutput& operator<<(TextOutput& to, const void* val) 115 | { 116 | char buf[16]; 117 | sprintf(buf, "%p", val); 118 | to.print(buf, strlen(buf)); 119 | return to; 120 | } 121 | 122 | static void textOutputPrinter(void* cookie, const char* txt) 123 | { 124 | ((TextOutput*)cookie)->print(txt, strlen(txt)); 125 | } 126 | 127 | TextOutput& operator<<(TextOutput& to, const TypeCode& val) 128 | { 129 | printTypeCode(val.typeCode(), textOutputPrinter, (void*)&to); 130 | return to; 131 | } 132 | 133 | HexDump::HexDump(const void *buf, size_t size, size_t bytesPerLine) 134 | : mBuffer(buf) 135 | , mSize(size) 136 | , mBytesPerLine(bytesPerLine) 137 | , mSingleLineCutoff(16) 138 | , mAlignment(4) 139 | , mCArrayStyle(false) 140 | { 141 | if (bytesPerLine >= 16) mAlignment = 4; 142 | else if (bytesPerLine >= 8) mAlignment = 2; 143 | else mAlignment = 1; 144 | } 145 | 146 | TextOutput& operator<<(TextOutput& to, const HexDump& val) 147 | { 148 | printHexData(0, val.buffer(), val.size(), val.bytesPerLine(), 149 | val.singleLineCutoff(), val.alignment(), val.carrayStyle(), 150 | textOutputPrinter, (void*)&to); 151 | return to; 152 | } 153 | 154 | }; // namespace android 155 | -------------------------------------------------------------------------------- /libs/utils/Timers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // 18 | // Timer functions. 19 | // 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #ifdef HAVE_WIN32_THREADS 32 | #include 33 | #endif 34 | 35 | nsecs_t systemTime(int clock) 36 | { 37 | #if defined(HAVE_POSIX_CLOCKS) 38 | static const clockid_t clocks[] = { 39 | CLOCK_REALTIME, 40 | CLOCK_MONOTONIC, 41 | CLOCK_PROCESS_CPUTIME_ID, 42 | CLOCK_THREAD_CPUTIME_ID 43 | }; 44 | struct timespec t; 45 | t.tv_sec = t.tv_nsec = 0; 46 | clock_gettime(clocks[clock], &t); 47 | return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; 48 | #else 49 | // we don't support the clocks here. 50 | struct timeval t; 51 | t.tv_sec = t.tv_usec = 0; 52 | gettimeofday(&t, NULL); 53 | return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; 54 | #endif 55 | } 56 | 57 | int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime) 58 | { 59 | int timeoutDelayMillis; 60 | if (timeoutTime > referenceTime) { 61 | uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime); 62 | if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) { 63 | timeoutDelayMillis = -1; 64 | } else { 65 | timeoutDelayMillis = (timeoutDelay + 999999LL) / 1000000LL; 66 | } 67 | } else { 68 | timeoutDelayMillis = 0; 69 | } 70 | return timeoutDelayMillis; 71 | } 72 | 73 | 74 | /* 75 | * =========================================================================== 76 | * DurationTimer 77 | * =========================================================================== 78 | */ 79 | 80 | using namespace android; 81 | 82 | // Start the timer. 83 | void DurationTimer::start(void) 84 | { 85 | gettimeofday(&mStartWhen, NULL); 86 | } 87 | 88 | // Stop the timer. 89 | void DurationTimer::stop(void) 90 | { 91 | gettimeofday(&mStopWhen, NULL); 92 | } 93 | 94 | // Get the duration in microseconds. 95 | long long DurationTimer::durationUsecs(void) const 96 | { 97 | return (long) subtractTimevals(&mStopWhen, &mStartWhen); 98 | } 99 | 100 | // Subtract two timevals. Returns the difference (ptv1-ptv2) in 101 | // microseconds. 102 | /*static*/ long long DurationTimer::subtractTimevals(const struct timeval* ptv1, 103 | const struct timeval* ptv2) 104 | { 105 | long long stop = ((long long) ptv1->tv_sec) * 1000000LL + 106 | ((long long) ptv1->tv_usec); 107 | long long start = ((long long) ptv2->tv_sec) * 1000000LL + 108 | ((long long) ptv2->tv_usec); 109 | return stop - start; 110 | } 111 | 112 | // Add the specified amount of time to the timeval. 113 | /*static*/ void DurationTimer::addToTimeval(struct timeval* ptv, long usec) 114 | { 115 | if (usec < 0) { 116 | LOG(LOG_WARN, "", "Negative values not supported in addToTimeval\n"); 117 | return; 118 | } 119 | 120 | // normalize tv_usec if necessary 121 | if (ptv->tv_usec >= 1000000) { 122 | ptv->tv_sec += ptv->tv_usec / 1000000; 123 | ptv->tv_usec %= 1000000; 124 | } 125 | 126 | ptv->tv_usec += usec % 1000000; 127 | if (ptv->tv_usec >= 1000000) { 128 | ptv->tv_usec -= 1000000; 129 | ptv->tv_sec++; 130 | } 131 | ptv->tv_sec += usec / 1000000; 132 | } 133 | 134 | -------------------------------------------------------------------------------- /libs/utils/misc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // 18 | // Miscellaneous utility functions. 19 | // 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | using namespace android; 29 | 30 | namespace android { 31 | 32 | /* 33 | * Like strdup(), but uses C++ "new" operator instead of malloc. 34 | */ 35 | char* strdupNew(const char* str) 36 | { 37 | char* newStr; 38 | int len; 39 | 40 | if (str == NULL) 41 | return NULL; 42 | 43 | len = strlen(str); 44 | newStr = new char[len+1]; 45 | memcpy(newStr, str, len+1); 46 | 47 | return newStr; 48 | } 49 | 50 | /* 51 | * Concatenate an argument vector. 52 | */ 53 | char* concatArgv(int argc, const char* const argv[]) 54 | { 55 | char* newStr = NULL; 56 | int len, totalLen, posn, idx; 57 | 58 | /* 59 | * First, figure out the total length. 60 | */ 61 | totalLen = idx = 0; 62 | while (1) { 63 | if (idx == argc || argv[idx] == NULL) 64 | break; 65 | if (idx) 66 | totalLen++; // leave a space between args 67 | totalLen += strlen(argv[idx]); 68 | idx++; 69 | } 70 | 71 | /* 72 | * Alloc the string. 73 | */ 74 | newStr = new char[totalLen +1]; 75 | if (newStr == NULL) 76 | return NULL; 77 | 78 | /* 79 | * Finally, allocate the string and copy data over. 80 | */ 81 | idx = posn = 0; 82 | while (1) { 83 | if (idx == argc || argv[idx] == NULL) 84 | break; 85 | if (idx) 86 | newStr[posn++] = ' '; 87 | 88 | len = strlen(argv[idx]); 89 | memcpy(&newStr[posn], argv[idx], len); 90 | posn += len; 91 | 92 | idx++; 93 | } 94 | 95 | assert(posn == totalLen); 96 | newStr[posn] = '\0'; 97 | 98 | return newStr; 99 | } 100 | 101 | /* 102 | * Count the #of args in an argument vector. Don't count the final NULL. 103 | */ 104 | int countArgv(const char* const argv[]) 105 | { 106 | int count = 0; 107 | 108 | while (argv[count] != NULL) 109 | count++; 110 | 111 | return count; 112 | } 113 | 114 | 115 | #include 116 | /* 117 | * Get a file's type. 118 | */ 119 | FileType getFileType(const char* fileName) 120 | { 121 | struct stat sb; 122 | 123 | if (stat(fileName, &sb) < 0) { 124 | if (errno == ENOENT || errno == ENOTDIR) 125 | return kFileTypeNonexistent; 126 | else { 127 | fprintf(stderr, "getFileType got errno=%d on '%s'\n", 128 | errno, fileName); 129 | return kFileTypeUnknown; 130 | } 131 | } else { 132 | if (S_ISREG(sb.st_mode)) 133 | return kFileTypeRegular; 134 | else if (S_ISDIR(sb.st_mode)) 135 | return kFileTypeDirectory; 136 | else if (S_ISCHR(sb.st_mode)) 137 | return kFileTypeCharDev; 138 | else if (S_ISBLK(sb.st_mode)) 139 | return kFileTypeBlockDev; 140 | else if (S_ISFIFO(sb.st_mode)) 141 | return kFileTypeFifo; 142 | #ifdef HAVE_SYMLINKS 143 | else if (S_ISLNK(sb.st_mode)) 144 | return kFileTypeSymlink; 145 | else if (S_ISSOCK(sb.st_mode)) 146 | return kFileTypeSocket; 147 | #endif 148 | else 149 | return kFileTypeUnknown; 150 | } 151 | } 152 | 153 | /* 154 | * Get a file's modification date. 155 | */ 156 | time_t getFileModDate(const char* fileName) 157 | { 158 | struct stat sb; 159 | 160 | if (stat(fileName, &sb) < 0) 161 | return (time_t) -1; 162 | 163 | return sb.st_mtime; 164 | } 165 | 166 | /* 167 | * Round up to the next highest power of 2. 168 | * 169 | * Found on http://graphics.stanford.edu/~seander/bithacks.html. 170 | */ 171 | unsigned int roundUpPower2(unsigned int val) 172 | { 173 | val--; 174 | val |= val >> 1; 175 | val |= val >> 2; 176 | val |= val >> 4; 177 | val |= val >> 8; 178 | val |= val >> 16; 179 | val++; 180 | 181 | return val; 182 | } 183 | 184 | }; // namespace android 185 | 186 | -------------------------------------------------------------------------------- /module/Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | obj-m := binder_new.o binder_old.o 3 | binder_old-y := deps.o binder.o 4 | binder_new-y := new/msg_queue.o new/binder.o 5 | 6 | $(obj)/deps.o: $(src)/deps.h 7 | 8 | $(obj)/deps.h: $(src)/gen_deps.sh 9 | $(src)/gen_deps.sh > $@ 10 | else 11 | KDIR ?= /lib/modules/`uname -r`/build 12 | 13 | all: 14 | $(MAKE) -C $(KDIR) V=0 M=$$PWD 15 | 16 | clean: 17 | rm -rf deps.h new/*.o *.o *.ko *.mod.c *.symvers *.order .*.cmd .tmp_versions 18 | endif 19 | -------------------------------------------------------------------------------- /module/deps.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "deps.h" 10 | 11 | static struct sighand_struct *(*__lock_task_sighand_ptr)(struct task_struct *, unsigned long *) = __LOCK_TASK_SIGHAND; 12 | static struct files_struct *(*get_files_struct_ptr)(struct task_struct *) = GET_FILES_STRUCT; 13 | static void (*put_files_struct_ptr)(struct files_struct *) = PUT_FILES_STRUCT; 14 | static void (*unmap_kernel_range_ptr)(unsigned long, unsigned long) = UNMAP_KERNEL_RANGE; 15 | static int (*expand_files_ptr)(struct files_struct *, int) = EXPAND_FILES; 16 | static unsigned long (*zap_page_range_ptr)(struct vm_area_struct *, unsigned long, unsigned long, struct zap_details *) = ZAP_PAGE_RANGE; 17 | static struct vm_struct *(*get_vm_area_ptr)(unsigned long, unsigned long) = GET_VM_AREA; 18 | static int (*can_nice_ptr)(const struct task_struct *, const int) = CAN_NICE; 19 | 20 | 21 | struct sighand_struct *__lock_task_sighand(struct task_struct *tsk, unsigned long *flags) 22 | { 23 | return __lock_task_sighand_ptr(tsk, flags); 24 | } 25 | 26 | struct files_struct *get_files_struct(struct task_struct *task) 27 | { 28 | return get_files_struct_ptr(task); 29 | } 30 | 31 | void put_files_struct(struct files_struct *files) 32 | { 33 | put_files_struct_ptr(files); 34 | } 35 | 36 | void unmap_kernel_range(unsigned long addr, unsigned long size) 37 | { 38 | unmap_kernel_range_ptr(addr, size); 39 | } 40 | 41 | int expand_files(struct files_struct *files, int nr) 42 | { 43 | return expand_files_ptr(files, nr); 44 | } 45 | 46 | unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, struct zap_details *details) 47 | { 48 | return zap_page_range_ptr(vma, address, size, details); 49 | } 50 | 51 | struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 52 | { 53 | return get_vm_area_ptr(size, flags); 54 | } 55 | 56 | int can_nice(const struct task_struct *p, const int nice) 57 | { 58 | return can_nice_ptr(p, nice); 59 | } 60 | -------------------------------------------------------------------------------- /module/gen_deps.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | SYMS="__lock_task_sighand get_files_struct put_files_struct unmap_kernel_range expand_files zap_page_range get_vm_area can_nice" 4 | for sym in $SYMS; do 5 | addr=`cat /proc/kallsyms | grep -Ee '^[0-9a-f]+ T '$sym'$' | sed -e 's/\s.*$//g'` 6 | if [ a$addr = 'a' ]; then 7 | echo "Error: can't find symbol $sym" 8 | exit 1 9 | fi 10 | 11 | name=`echo $sym | tr '[:lower:]' '[:upper:]'` 12 | echo "#define $name\t(void *)0x$addr" 13 | done 14 | -------------------------------------------------------------------------------- /module/new/fast_slob.h: -------------------------------------------------------------------------------- 1 | /* 2 | * fast_slob.h: a simple but fast linked-list based buffer allocator 3 | * Copyright (c) 2012 Rong Shen 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 | */ 18 | #ifndef _FAST_SLOB_H 19 | #define _FAST_SLOB_H 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | 28 | #define MIN_ALLOC_SIZE sizeof(char *) 29 | 30 | 31 | struct fast_slob { 32 | spinlock_t lock; 33 | 34 | size_t bucket_size; 35 | int min_alloc_size; 36 | int max_alloc_size; 37 | int alloc_size_shift; 38 | int num_buckets; 39 | 40 | char *start, *end; 41 | 42 | char *buckets[0]; 43 | }; 44 | 45 | 46 | static inline struct fast_slob *fast_slob_create(size_t size, int max_alloc_size, int alloc_size_shift, int num_buckets) 47 | { 48 | struct fast_slob *slob; 49 | size_t bucket_size, min_alloc_size; 50 | char *start, *end, *buf; 51 | int i; 52 | 53 | if (max_alloc_size < MIN_ALLOC_SIZE || alloc_size_shift < 1 || num_buckets < 1) 54 | return NULL; 55 | 56 | bucket_size = L1_CACHE_ALIGN(size / num_buckets); 57 | if (bucket_size * num_buckets > size) 58 | bucket_size = (size / num_buckets) & ~(L1_CACHE_BYTES - 1); 59 | if (bucket_size < max_alloc_size) 60 | return NULL; 61 | 62 | min_alloc_size = max_alloc_size >> (alloc_size_shift * (num_buckets - 1)); 63 | if (min_alloc_size < MIN_ALLOC_SIZE) 64 | return NULL; 65 | 66 | slob = kmalloc(sizeof(*slob) + num_buckets * sizeof(char *), GFP_KERNEL); 67 | if (!slob) 68 | return NULL; 69 | 70 | slob->start = vmalloc_user(size); 71 | if (!slob->start) { 72 | kfree(slob); 73 | return NULL; 74 | } 75 | 76 | slob->end = slob->start + size; 77 | slob->bucket_size = bucket_size; 78 | slob->min_alloc_size = min_alloc_size; 79 | slob->max_alloc_size = max_alloc_size; 80 | slob->alloc_size_shift = alloc_size_shift; 81 | slob->num_buckets = num_buckets; 82 | 83 | for (i = 0; i < num_buckets; i++) { 84 | start = slob->start + i * bucket_size; 85 | end = start + bucket_size; 86 | slob->buckets[i] = start; 87 | 88 | while (start < end) { 89 | buf = start; 90 | start += ALIGN(min_alloc_size, MIN_ALLOC_SIZE); 91 | *(char **)buf = (start < end) ? start : NULL; 92 | } 93 | 94 | min_alloc_size <<= alloc_size_shift; 95 | } 96 | 97 | spin_lock_init(&slob->lock); 98 | return slob; 99 | } 100 | 101 | static inline void fast_slob_destroy(struct fast_slob *slob) 102 | { 103 | vfree(slob->start); 104 | kfree(slob); 105 | } 106 | 107 | static inline void *fast_slob_alloc(struct fast_slob *slob, size_t size) 108 | { 109 | size_t alloc_size = slob->min_alloc_size; 110 | char *p; 111 | int i; 112 | 113 | spin_lock(&slob->lock); 114 | for (i = 0; i < slob->num_buckets; i++) { 115 | if (alloc_size >= size && slob->buckets[i]) { 116 | p = slob->buckets[i]; 117 | slob->buckets[i] = *(char **)p; 118 | spin_unlock(&slob->lock); 119 | return p; 120 | } 121 | alloc_size <<= slob->alloc_size_shift; 122 | } 123 | spin_unlock(&slob->lock); 124 | 125 | return NULL; 126 | } 127 | 128 | static inline int fast_slob_bucket(struct fast_slob *slob, void *p) 129 | { 130 | size_t off, alloc_size; 131 | int idx; 132 | 133 | if ((char *)p < slob->start || (char *)p >= slob->end) 134 | return -1; 135 | 136 | off = (char *)p - slob->start; 137 | idx = off / slob->bucket_size; 138 | alloc_size = slob->min_alloc_size << (idx * slob->alloc_size_shift); 139 | if ((off - idx * slob->bucket_size) % ALIGN(alloc_size, MIN_ALLOC_SIZE)) 140 | return -1; 141 | 142 | return idx; 143 | } 144 | 145 | static inline void _fast_slob_free(struct fast_slob *slob, int idx, void *p) 146 | { 147 | spin_lock(&slob->lock); 148 | *(char **)p = slob->buckets[idx]; 149 | slob->buckets[idx] = p; 150 | spin_unlock(&slob->lock); 151 | } 152 | 153 | static inline void fast_slob_free(struct fast_slob *slob, void *p) 154 | { 155 | int idx; 156 | 157 | if ((idx = fast_slob_bucket(slob, p)) < 0) { 158 | printk(KERN_WARNING "fast_slob: try to free an invalid buffer with address %p\n", p); 159 | return; 160 | } 161 | 162 | _fast_slob_free(slob, idx, p); 163 | } 164 | 165 | #endif /* _FAST_SLOB_H */ 166 | -------------------------------------------------------------------------------- /module/new/inst.h: -------------------------------------------------------------------------------- 1 | #ifndef _INST_H 2 | #define _INST_H 3 | 4 | #undef KERNEL_INSTRUMENTING 5 | 6 | #ifdef KERNEL_INSTRUMENTING 7 | #include 8 | 9 | typedef union { 10 | struct timeval tv; 11 | char label[8]; 12 | } inst_entry_t; 13 | 14 | typedef struct { 15 | uint32_t magic; 16 | uint32_t seq; 17 | uint32_t max_entries; 18 | uint32_t next_entry; 19 | inst_entry_t entries[0]; 20 | } inst_buf_t; 21 | 22 | 23 | static inline void __inst_entry(void *ptr, char *label, struct timeval *copy) 24 | { 25 | inst_buf_t *inst = (inst_buf_t *)ptr; 26 | 27 | if (inst->magic != 0x696e7374) 28 | return; 29 | 30 | if (inst->next_entry < inst->max_entries) { 31 | inst_entry_t *entry = inst->entries + inst->next_entry++; 32 | 33 | if (inst->seq) { 34 | if (!copy) 35 | do_gettimeofday(&entry->tv); 36 | else 37 | entry->tv = *copy; 38 | } else { 39 | strncpy(entry->label, label, 8); 40 | entry->label[7] = '\0'; 41 | } 42 | } 43 | } 44 | #define INST_ENTRY(p, label) __inst_entry((p), (label), NULL) 45 | #define INST_ENTRY_COPY(t, p, label, n) __inst_entry((p), (label), (t)->__inst_copies + (n)) 46 | #define INST_RECORD(t, n) do_gettimeofday((t)->__inst_copies + (n)); 47 | #else 48 | #define INST_ENTRY(b, label) 49 | #define INST_ENTRY_COPY(t, p, label, n) 50 | #define INST_RECORD(t, n) 51 | #endif /* KERNEL_INSTRUMENTING */ 52 | 53 | #endif /* _INST_H */ 54 | -------------------------------------------------------------------------------- /module/new/msg_queue.c: -------------------------------------------------------------------------------- 1 | /* 2 | * msg_queue.c: a generic process messaging queue implementation 3 | * Copyright (c) 2012 Rong Shen 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 | */ 18 | #include 19 | #include 20 | 21 | #include "msg_queue.h" 22 | 23 | 24 | static DEFINE_SPINLOCK(g_queue_lock); 25 | static struct rb_root g_queue_tree = RB_ROOT; 26 | static msg_queue_id g_queue_seq; 27 | 28 | 29 | static inline void rb_insert_queue(struct msg_queue *new) 30 | { 31 | struct rb_node *parent, **p; 32 | struct msg_queue *q; 33 | msg_queue_id id; 34 | 35 | retry: 36 | id = ++g_queue_seq; 37 | p = &g_queue_tree.rb_node; 38 | parent = NULL; 39 | 40 | while (*p) { 41 | parent = *p; 42 | q = container_of(parent, struct msg_queue, rb_node); 43 | 44 | if (id < q->id) 45 | p = &(*p)->rb_left; 46 | else if (id > q->id) 47 | p = &(*p)->rb_right; 48 | else 49 | goto retry; 50 | } 51 | 52 | new->id = id; 53 | rb_link_node(&new->rb_node, parent, p); 54 | rb_insert_color(&new->rb_node, &g_queue_tree); 55 | } 56 | 57 | static inline struct msg_queue *rb_queue_exist(msg_queue_id id) 58 | { 59 | struct rb_node *n = g_queue_tree.rb_node; 60 | struct msg_queue *q; 61 | 62 | while (n) { 63 | q = container_of(n, struct msg_queue, rb_node); 64 | 65 | if (id < q->id) 66 | n = n->rb_left; 67 | else if (id > q->id) 68 | n = n->rb_right; 69 | else 70 | return q; 71 | } 72 | 73 | return NULL; 74 | } 75 | 76 | struct msg_queue *create_msg_queue(size_t max_msgs, queue_release_handler handler, void *data) 77 | { 78 | struct msg_queue *q; 79 | 80 | q = kmalloc(sizeof(*q), GFP_KERNEL); 81 | if (!q) 82 | return NULL; 83 | 84 | q->max_msgs = (max_msgs > 0) ? max_msgs : DEFAULT_MAX_QUEUE_LENGTH; 85 | q->num_msgs = 0; 86 | 87 | spin_lock_init(&q->lock); 88 | INIT_LIST_HEAD(&q->msgs); 89 | init_waitqueue_head(&q->rd_wait); 90 | init_waitqueue_head(&q->wr_wait); 91 | 92 | q->active = 1; 93 | q->usage = 1; 94 | q->release = handler; 95 | q->private = data; 96 | 97 | spin_lock(&g_queue_lock); 98 | rb_insert_queue(q); 99 | spin_unlock(&g_queue_lock); 100 | return q; 101 | } 102 | 103 | int free_msg_queue(struct msg_queue *q) 104 | { 105 | q->active = 0; 106 | return put_msg_queue(q); 107 | } 108 | 109 | struct msg_queue *get_msg_queue(msg_queue_id id) 110 | { 111 | struct msg_queue *q; 112 | 113 | spin_lock(&g_queue_lock); 114 | if (!(q = rb_queue_exist(id))) { 115 | spin_unlock(&g_queue_lock); 116 | return NULL; 117 | } 118 | 119 | q->usage++; 120 | spin_unlock(&g_queue_lock); 121 | 122 | return q; 123 | } 124 | 125 | int put_msg_queue(struct msg_queue *q) 126 | { 127 | spin_lock(&g_queue_lock); 128 | 129 | if (--q->usage > 0) { 130 | spin_unlock(&g_queue_lock); 131 | return 0; 132 | } 133 | 134 | rb_erase(&q->rb_node, &g_queue_tree); 135 | spin_unlock(&g_queue_lock); 136 | 137 | BUG_ON(waitqueue_active(&q->rd_wait) || waitqueue_active(&q->wr_wait)); 138 | 139 | if (q->release) 140 | q->release(q, q->private); 141 | kfree(q); 142 | 143 | return 1; 144 | } 145 | 146 | static int _write_msg_queue(struct msg_queue *q, struct list_head *msg, int head) 147 | { 148 | DECLARE_WAITQUEUE(wait, current); 149 | int r; 150 | 151 | add_wait_queue(&q->wr_wait, &wait); 152 | do { 153 | set_current_state(TASK_INTERRUPTIBLE); 154 | 155 | if (!q->active) { 156 | r = -EIO; 157 | break; 158 | } 159 | 160 | spin_lock(&q->lock); 161 | if (q->num_msgs < q->max_msgs) { 162 | if (head) 163 | list_add(msg, &q->msgs); 164 | else 165 | list_add_tail(msg, &q->msgs); 166 | q->num_msgs++; 167 | spin_unlock(&q->lock); 168 | 169 | wake_up(&q->rd_wait); 170 | r = 0; 171 | break; 172 | } 173 | spin_unlock(&q->lock); 174 | 175 | if (signal_pending(current)) { 176 | r = -ERESTARTSYS; 177 | break; 178 | } 179 | schedule(); 180 | } while (1); 181 | 182 | __set_current_state(TASK_RUNNING); 183 | remove_wait_queue(&q->wr_wait, &wait); 184 | 185 | return r; 186 | } 187 | 188 | int write_msg_queue(struct msg_queue *q, struct list_head *msg) 189 | { 190 | return _write_msg_queue(q, msg, 0); 191 | } 192 | 193 | int write_msg_queue_head(struct msg_queue *q, struct list_head *msg) 194 | { 195 | return _write_msg_queue(q, msg, 1); 196 | } 197 | 198 | static int _read_msg_queue(struct msg_queue *q, struct list_head **pmsg, int tail) 199 | { 200 | struct list_head *entry; 201 | DECLARE_WAITQUEUE(wait, current); 202 | int r; 203 | 204 | add_wait_queue(&q->rd_wait, &wait); 205 | do { 206 | set_current_state(TASK_INTERRUPTIBLE); 207 | 208 | if (!q->active) { 209 | r = -EIO; 210 | break; 211 | } 212 | 213 | spin_lock(&q->lock); 214 | if (q->num_msgs > 0) { 215 | if (tail) 216 | entry = q->msgs.prev; 217 | else 218 | entry = q->msgs.next; 219 | 220 | list_del(entry); 221 | q->num_msgs--; 222 | spin_unlock(&q->lock); 223 | 224 | *pmsg = entry; 225 | 226 | wake_up(&q->wr_wait); 227 | r = 0; 228 | break; 229 | } 230 | spin_unlock(&q->lock); 231 | 232 | if (signal_pending(current)) { 233 | r = -ERESTARTSYS; 234 | break; 235 | } 236 | schedule(); 237 | } while (1); 238 | 239 | __set_current_state(TASK_RUNNING); 240 | remove_wait_queue(&q->rd_wait, &wait); 241 | 242 | return r; 243 | } 244 | 245 | int read_msg_queue(struct msg_queue *q, struct list_head **pmsg) 246 | { 247 | return _read_msg_queue(q, pmsg, 0); 248 | } 249 | 250 | int read_msg_queue_tail(struct msg_queue *q, struct list_head **pmsg) 251 | { 252 | return _read_msg_queue(q, pmsg, 1); 253 | } 254 | -------------------------------------------------------------------------------- /module/new/msg_queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * msg_queue.h: a generic process messaging queue implementation 3 | * Copyright (c) 2012 Rong Shen 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 | */ 18 | #ifndef _MSG_QUEUE_H 19 | #define _MSG_QUEUE_H 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | 29 | #define DEFAULT_MAX_QUEUE_LENGTH 100 30 | 31 | 32 | struct msg_queue; 33 | 34 | typedef unsigned long msg_queue_id; 35 | typedef void (*queue_release_handler)(struct msg_queue *q, void *); 36 | 37 | 38 | struct msg_queue { 39 | msg_queue_id id; 40 | 41 | spinlock_t lock; 42 | int active; 43 | 44 | size_t num_msgs, max_msgs; 45 | struct list_head msgs; 46 | 47 | wait_queue_head_t rd_wait; 48 | wait_queue_head_t wr_wait; 49 | 50 | struct rb_node rb_node; 51 | int usage; 52 | 53 | queue_release_handler release; 54 | void *private; 55 | }; 56 | 57 | 58 | extern struct msg_queue *create_msg_queue(size_t max_msgs, queue_release_handler handler, void *data); 59 | extern int free_msg_queue(struct msg_queue *q); 60 | 61 | extern struct msg_queue *get_msg_queue(msg_queue_id id); 62 | extern int put_msg_queue(struct msg_queue *q); 63 | 64 | extern int write_msg_queue(struct msg_queue *q, struct list_head *msg); 65 | extern int write_msg_queue_head(struct msg_queue *q, struct list_head *msg); 66 | 67 | extern int read_msg_queue(struct msg_queue *q, struct list_head **pmsg); 68 | extern int read_msg_queue_tail(struct msg_queue *q, struct list_head **pmsg); 69 | 70 | 71 | #define msg_queue_id(q) (q)->id 72 | 73 | /* Following inline functions should be called either by the queue owner or 74 | * with a reference held via a call to get_msg_queue() previously 75 | */ 76 | static inline void enable_msg_queue(struct msg_queue *q) 77 | { 78 | q->active = 1; 79 | } 80 | 81 | static inline void disable_msg_queue(struct msg_queue *q) 82 | { 83 | q->active = 0; 84 | } 85 | 86 | static inline int msg_queue_active(struct msg_queue *q) 87 | { 88 | return (q->active > 0); 89 | } 90 | 91 | static inline int msg_queue_empty(struct msg_queue *q) 92 | { 93 | return (q->num_msgs < 1); 94 | } 95 | 96 | static inline int msg_queue_full(struct msg_queue *q) 97 | { 98 | return (q->num_msgs >= q->max_msgs); 99 | } 100 | 101 | static inline size_t msg_queue_size(struct msg_queue *q) 102 | { 103 | return q->num_msgs; 104 | } 105 | 106 | // Unsafe! Only use it when no one else is accessing the queue 107 | static inline struct list_head *msg_queue_pop(struct msg_queue *q) 108 | { 109 | struct list_head *next = q->msgs.next; 110 | 111 | if (next != &q->msgs) { 112 | list_del(next); 113 | return next; 114 | } else 115 | return NULL; 116 | } 117 | 118 | static inline void msg_queue_poll_wait(struct msg_queue *q, struct file *filp, poll_table *p) 119 | { 120 | poll_wait(filp, &q->rd_wait, p); 121 | poll_wait(filp, &q->wr_wait, p); 122 | } 123 | 124 | static inline void msg_queue_poll_wait_read(struct msg_queue *q, struct file *filp, poll_table *p) 125 | { 126 | poll_wait(filp, &q->rd_wait, p); 127 | } 128 | 129 | static inline void msg_queue_poll_wait_write(struct msg_queue *q, struct file *filp, poll_table *p) 130 | { 131 | poll_wait(filp, &q->wr_wait, p); 132 | } 133 | #endif /* _MSG_QUEUE_H */ 134 | -------------------------------------------------------------------------------- /servicemanager/Makefile: -------------------------------------------------------------------------------- 1 | all: servicemanager 2 | 3 | #CFLAGS := -DINLINE_TRANSACTION_DATA 4 | 5 | servicemanager: binder.o service_manager.o 6 | gcc -o $@ $^ 7 | 8 | clean: 9 | rm -f *.o servicemanager 10 | 11 | %.o: %.c 12 | gcc -I.. $(CFLAGS) -c -o $@ $< 13 | -------------------------------------------------------------------------------- /servicemanager/bctest.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2008 The Android Open Source Project 2 | */ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "binder.h" 9 | 10 | void *svcmgr_lookup(struct binder_state *bs, void *target, const char *name) 11 | { 12 | void *ptr; 13 | unsigned iodata[512/4]; 14 | struct binder_io msg, reply; 15 | 16 | bio_init(&msg, iodata, sizeof(iodata), 4); 17 | bio_put_uint32(&msg, 0); // strict mode header 18 | bio_put_string16_x(&msg, SVC_MGR_NAME); 19 | bio_put_string16_x(&msg, name); 20 | 21 | if (binder_call(bs, &msg, &reply, target, SVC_MGR_CHECK_SERVICE)) 22 | return 0; 23 | 24 | ptr = bio_get_ref(&reply); 25 | 26 | if (ptr) 27 | binder_acquire(bs, ptr); 28 | 29 | binder_done(bs, &msg, &reply); 30 | 31 | return ptr; 32 | } 33 | 34 | int svcmgr_publish(struct binder_state *bs, void *target, const char *name, void *ptr) 35 | { 36 | unsigned status; 37 | unsigned iodata[512/4]; 38 | struct binder_io msg, reply; 39 | 40 | bio_init(&msg, iodata, sizeof(iodata), 4); 41 | bio_put_uint32(&msg, 0); // strict mode header 42 | bio_put_string16_x(&msg, SVC_MGR_NAME); 43 | bio_put_string16_x(&msg, name); 44 | bio_put_obj(&msg, ptr); 45 | 46 | if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE)) 47 | return -1; 48 | 49 | status = bio_get_uint32(&reply); 50 | 51 | binder_done(bs, &msg, &reply); 52 | 53 | return status; 54 | } 55 | 56 | unsigned token; 57 | 58 | int main(int argc, char **argv) 59 | { 60 | int fd; 61 | struct binder_state *bs; 62 | void *svcmgr = BINDER_SERVICE_MANAGER; 63 | 64 | bs = binder_open(128*1024); 65 | 66 | argc--; 67 | argv++; 68 | while (argc > 0) { 69 | if (!strcmp(argv[0],"alt")) { 70 | void *ptr = svcmgr_lookup(bs, svcmgr, "alt_svc_mgr"); 71 | if (!ptr) { 72 | fprintf(stderr,"cannot find alt_svc_mgr\n"); 73 | return -1; 74 | } 75 | svcmgr = ptr; 76 | fprintf(stderr,"svcmgr is via %p\n", ptr); 77 | } else if (!strcmp(argv[0],"lookup")) { 78 | void *ptr; 79 | if (argc < 2) { 80 | fprintf(stderr,"argument required\n"); 81 | return -1; 82 | } 83 | ptr = svcmgr_lookup(bs, svcmgr, argv[1]); 84 | fprintf(stderr,"lookup(%s) = %p\n", argv[1], ptr); 85 | argc--; 86 | argv++; 87 | } else if (!strcmp(argv[0],"publish")) { 88 | if (argc < 2) { 89 | fprintf(stderr,"argument required\n"); 90 | return -1; 91 | } 92 | svcmgr_publish(bs, svcmgr, argv[1], &token); 93 | argc--; 94 | argv++; 95 | } else { 96 | fprintf(stderr,"unknown command %s\n", argv[0]); 97 | return -1; 98 | } 99 | argc--; 100 | argv++; 101 | } 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /servicemanager/binder.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2008 The Android Open Source Project 2 | */ 3 | 4 | #ifndef _BINDER_H_ 5 | #define _BINDER_H_ 6 | 7 | #ifndef uint8_t 8 | typedef unsigned char uint8_t; 9 | #endif 10 | 11 | #ifndef uint16_t 12 | typedef unsigned short uint16_t; 13 | #endif 14 | 15 | #ifndef uint32_t 16 | typedef unsigned int uint32_t; 17 | #endif 18 | 19 | #ifndef uint64_t 20 | typedef struct _uint64_t { 21 | uint32_t low_dw; 22 | uint32_t hi_dw; 23 | } uint64_t; 24 | #endif 25 | 26 | #include 27 | 28 | #include "module/binder.h" 29 | 30 | struct binder_state; 31 | 32 | struct binder_object 33 | { 34 | uint32_t type; 35 | uint32_t flags; 36 | void *pointer; 37 | void *cookie; 38 | }; 39 | 40 | struct binder_txn 41 | { 42 | void *target; 43 | void *cookie; 44 | uint32_t code; 45 | uint32_t flags; 46 | 47 | uint32_t sender_pid; 48 | uint32_t sender_euid; 49 | 50 | uint32_t data_size; 51 | uint32_t offs_size; 52 | void *data; 53 | void *offs; 54 | }; 55 | 56 | struct binder_io 57 | { 58 | char *data; /* pointer to read/write from */ 59 | uint32_t *offs; /* array of offsets */ 60 | uint32_t data_avail; /* bytes available in data buffer */ 61 | uint32_t offs_avail; /* entries available in offsets array */ 62 | 63 | char *data0; /* start of data buffer */ 64 | uint32_t *offs0; /* start of offsets buffer */ 65 | uint32_t flags; 66 | uint32_t unused; 67 | }; 68 | 69 | struct binder_death { 70 | void (*func)(struct binder_state *bs, void *ptr); 71 | void *ptr; 72 | }; 73 | 74 | /* the one magic object */ 75 | #define BINDER_SERVICE_MANAGER ((void*) 0) 76 | 77 | #define SVC_MGR_NAME "android.os.IServiceManager" 78 | 79 | enum { 80 | SVC_MGR_GET_SERVICE = 1, 81 | SVC_MGR_CHECK_SERVICE, 82 | SVC_MGR_ADD_SERVICE, 83 | SVC_MGR_LIST_SERVICES, 84 | }; 85 | 86 | typedef int (*binder_handler)(struct binder_state *bs, 87 | struct binder_txn *txn, 88 | struct binder_io *msg, 89 | struct binder_io *reply); 90 | 91 | struct binder_state *binder_open(unsigned mapsize); 92 | void binder_close(struct binder_state *bs); 93 | 94 | /* initiate a blocking binder call 95 | * - returns zero on success 96 | */ 97 | int binder_call(struct binder_state *bs, 98 | struct binder_io *msg, struct binder_io *reply, 99 | void *target, uint32_t code); 100 | 101 | /* release any state associate with the binder_io 102 | * - call once any necessary data has been extracted from the 103 | * binder_io after binder_call() returns 104 | * - can safely be called even if binder_call() fails 105 | */ 106 | void binder_done(struct binder_state *bs, 107 | struct binder_io *msg, struct binder_io *reply); 108 | 109 | /* manipulate strong references */ 110 | void binder_acquire(struct binder_state *bs, void *ptr); 111 | void binder_release(struct binder_state *bs, void *ptr); 112 | 113 | void binder_link_to_death(struct binder_state *bs, void *ptr, struct binder_death *death); 114 | 115 | void binder_loop(struct binder_state *bs, binder_handler func); 116 | 117 | int binder_become_context_manager(struct binder_state *bs); 118 | 119 | /* allocate a binder_io, providing a stack-allocated working 120 | * buffer, size of the working buffer, and how many object 121 | * offset entries to reserve from the buffer 122 | */ 123 | void bio_init(struct binder_io *bio, void *data, 124 | uint32_t maxdata, uint32_t maxobjects); 125 | 126 | void bio_destroy(struct binder_io *bio); 127 | 128 | void bio_put_obj(struct binder_io *bio, void *ptr); 129 | void bio_put_ref(struct binder_io *bio, void *ptr); 130 | void bio_put_uint32(struct binder_io *bio, uint32_t n); 131 | void bio_put_string16(struct binder_io *bio, const uint16_t *str); 132 | void bio_put_string16_x(struct binder_io *bio, const char *_str); 133 | 134 | uint32_t bio_get_uint32(struct binder_io *bio); 135 | uint16_t *bio_get_string16(struct binder_io *bio, uint32_t *sz); 136 | void *bio_get_obj(struct binder_io *bio); 137 | void *bio_get_ref(struct binder_io *bio); 138 | 139 | #endif 140 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | all: binder_tester binderAddInts server client 2 | 3 | CFLAGS := #-DINLINE_TRANSACTION_DATA #-DSIMULATE_FREE_BUFFER 4 | 5 | binder_tester: binder_tester.c 6 | gcc $(CFLAGS) -Wall -o $@ -I../module $< 7 | 8 | binderAddInts: binderAddInts.cpp 9 | g++ -o $@ -I../libs/include -L../libs $< -lpthread -lbinder -lrt 10 | 11 | server: server.c 12 | gcc -o $@ -I../module/new $< 13 | 14 | client: client.c 15 | gcc -o $@ -I../module/new $< 16 | 17 | clean: 18 | rm -f binder_tester binderAddInts server client 19 | -------------------------------------------------------------------------------- /test/data-31jan2012.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rong1129/android-binder-ipc/41e34024117877a7b06dc7d7ae07e4cf4354081a/test/data-31jan2012.tgz --------------------------------------------------------------------------------