camerad/MemoryManager: remove lock and reduce redundant lookups (#34656)

remove lock and reduce redundant lookups
pull/34661/head
Dean Lee 2 months ago committed by GitHub
parent 2c56f44380
commit 839c8fbd6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      system/camerad/cameras/spectra.cc
  2. 2
      system/camerad/cameras/spectra.h

@ -137,26 +137,23 @@ static cam_cmd_power *power_set_wait(cam_cmd_power *power, int16_t delay_ms) {
// *** MemoryManager *** // *** MemoryManager ***
void *MemoryManager::alloc_buf(int size, uint32_t *handle) { void *MemoryManager::alloc_buf(int size, uint32_t *handle) {
lock.lock();
void *ptr; void *ptr;
if (!cached_allocations[size].empty()) { auto &cache = cached_allocations[size];
ptr = cached_allocations[size].front(); if (!cache.empty()) {
cached_allocations[size].pop(); ptr = cache.front();
cache.pop();
*handle = handle_lookup[ptr]; *handle = handle_lookup[ptr];
} else { } else {
ptr = alloc_w_mmu_hdl(video0_fd, size, handle); ptr = alloc_w_mmu_hdl(video0_fd, size, handle);
handle_lookup[ptr] = *handle; handle_lookup[ptr] = *handle;
size_lookup[ptr] = size; size_lookup[ptr] = size;
} }
lock.unlock();
memset(ptr, 0, size); memset(ptr, 0, size);
return ptr; return ptr;
} }
void MemoryManager::free(void *ptr) { void MemoryManager::free(void *ptr) {
lock.lock();
cached_allocations[size_lookup[ptr]].push(ptr); cached_allocations[size_lookup[ptr]].push(ptr);
lock.unlock();
} }
MemoryManager::~MemoryManager() { MemoryManager::~MemoryManager() {

@ -3,7 +3,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <mutex>
#include <queue> #include <queue>
#include <optional> #include <optional>
#include <utility> #include <utility>
@ -59,7 +58,6 @@ private:
void *alloc_buf(int len, uint32_t *handle); void *alloc_buf(int len, uint32_t *handle);
void free(void *ptr); void free(void *ptr);
std::mutex lock;
std::map<void *, uint32_t> handle_lookup; std::map<void *, uint32_t> handle_lookup;
std::map<void *, int> size_lookup; std::map<void *, int> size_lookup;
std::map<int, std::queue<void *> > cached_allocations; std::map<int, std::queue<void *> > cached_allocations;

Loading…
Cancel
Save