| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/ingestion/item_mem.cc |
| Date: | 2025-10-26 02:35:25 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 45 | 46 | 97.8% |
| Branches: | 34 | 50 | 68.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "item_mem.h" | ||
| 6 | |||
| 7 | #include <cassert> | ||
| 8 | #include <cstdlib> | ||
| 9 | |||
| 10 | #include "util/concurrency.h" | ||
| 11 | #include "util/exception.h" | ||
| 12 | |||
| 13 | atomic_int64 ItemAllocator::total_allocated_ = 0; | ||
| 14 | |||
| 15 | |||
| 16 | 14619624 | void ItemAllocator::Free(void *ptr) { | |
| 17 | 14619624 | const MutexLockGuard guard(lock_); | |
| 18 | |||
| 19 | 14660612 | MallocArena *M = MallocArena::GetMallocArena(ptr, kArenaSize); | |
| 20 |
1/2✓ Branch 1 taken 14660612 times.
✗ Branch 2 not taken.
|
14660612 | M->Free(ptr); |
| 21 | 14660612 | const unsigned N = malloc_arenas_.size(); | |
| 22 |
6/6✓ Branch 0 taken 2892996 times.
✓ Branch 1 taken 11767616 times.
✓ Branch 3 taken 74 times.
✓ Branch 4 taken 2892922 times.
✓ Branch 5 taken 74 times.
✓ Branch 6 taken 14660538 times.
|
14660612 | if ((N > 1) && M->IsEmpty()) { |
| 23 |
1/2✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
|
110 | for (unsigned i = 0; i < N; ++i) { |
| 24 |
2/2✓ Branch 1 taken 74 times.
✓ Branch 2 taken 36 times.
|
110 | if (malloc_arenas_[i] == M) { |
| 25 |
1/2✓ Branch 1 taken 74 times.
✗ Branch 2 not taken.
|
74 | delete malloc_arenas_[i]; |
| 26 | 74 | atomic_xadd64(&total_allocated_, -static_cast<int>(kArenaSize)); | |
| 27 |
1/2✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
|
74 | malloc_arenas_.erase(malloc_arenas_.begin() + i); |
| 28 | 74 | idx_last_arena_ = 0; | |
| 29 | 74 | return; | |
| 30 | } | ||
| 31 | } | ||
| 32 | ✗ | PANIC(NULL); | |
| 33 | } | ||
| 34 |
2/2✓ Branch 1 taken 14649430 times.
✓ Branch 2 taken 902 times.
|
14660612 | } |
| 35 | |||
| 36 | |||
| 37 | 1566 | ItemAllocator::ItemAllocator() : idx_last_arena_(0) { | |
| 38 | 1566 | const int retval = pthread_mutex_init(&lock_, NULL); | |
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1566 times.
|
1566 | assert(retval == 0); |
| 40 | |||
| 41 |
3/6✓ Branch 1 taken 1566 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1566 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1566 times.
✗ Branch 8 not taken.
|
1566 | malloc_arenas_.push_back(new MallocArena(kArenaSize)); |
| 42 | 1566 | atomic_xadd64(&total_allocated_, kArenaSize); | |
| 43 | 1566 | } | |
| 44 | |||
| 45 | |||
| 46 | 1565 | ItemAllocator::~ItemAllocator() { | |
| 47 |
2/2✓ Branch 1 taken 1565 times.
✓ Branch 2 taken 1565 times.
|
3130 | for (unsigned i = 0; i < malloc_arenas_.size(); ++i) { |
| 48 | 1565 | atomic_xadd64(&total_allocated_, -static_cast<int>(kArenaSize)); | |
| 49 |
1/2✓ Branch 1 taken 1565 times.
✗ Branch 2 not taken.
|
1565 | delete malloc_arenas_[i]; |
| 50 | } | ||
| 51 | 1565 | pthread_mutex_destroy(&lock_); | |
| 52 | 1565 | } | |
| 53 | |||
| 54 | |||
| 55 | 14594304 | void *ItemAllocator::Malloc(unsigned size) { | |
| 56 | 14594304 | const MutexLockGuard guard(lock_); | |
| 57 | |||
| 58 |
1/2✓ Branch 2 taken 14660612 times.
✗ Branch 3 not taken.
|
14660612 | void *p = malloc_arenas_[idx_last_arena_]->Malloc(size); |
| 59 |
2/2✓ Branch 0 taken 14660484 times.
✓ Branch 1 taken 128 times.
|
14660612 | if (p != NULL) |
| 60 | 14660484 | return p; | |
| 61 | 128 | const unsigned N = malloc_arenas_.size(); | |
| 62 |
2/2✓ Branch 0 taken 164 times.
✓ Branch 1 taken 74 times.
|
238 | for (unsigned i = 0; i < N; ++i) { |
| 63 |
1/2✓ Branch 2 taken 164 times.
✗ Branch 3 not taken.
|
164 | p = malloc_arenas_[i]->Malloc(size); |
| 64 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 110 times.
|
164 | if (p != NULL) { |
| 65 | 54 | idx_last_arena_ = i; | |
| 66 | 54 | return p; | |
| 67 | } | ||
| 68 | } | ||
| 69 | 74 | idx_last_arena_ = N; | |
| 70 |
2/4✓ Branch 1 taken 74 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
|
74 | MallocArena *M = new MallocArena(kArenaSize); |
| 71 | 74 | atomic_xadd64(&total_allocated_, kArenaSize); | |
| 72 |
1/2✓ Branch 1 taken 74 times.
✗ Branch 2 not taken.
|
74 | malloc_arenas_.push_back(M); |
| 73 |
1/2✓ Branch 1 taken 74 times.
✗ Branch 2 not taken.
|
74 | p = M->Malloc(size); |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
|
74 | assert(p != NULL); |
| 75 | 74 | return p; | |
| 76 | 14660612 | } | |
| 77 |