The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2005 The Android Open Source Project |
| 3 | // |
| 4 | // Hold a collection of log messages, limiting ourselves to a certain |
| 5 | // fixed maximum amount of memory. |
| 6 | // |
| 7 | #include "LogPool.h" |
| 8 | #include <assert.h> |
| 9 | |
| 10 | |
| 11 | /* |
| 12 | * Add a message at the head of the pool. |
| 13 | */ |
| 14 | void LogPool::Add(LogMessage* pLogMessage) |
| 15 | { |
| 16 | pLogMessage->Acquire(); // bump up the ref count |
| 17 | |
| 18 | assert(pLogMessage->GetPrev() == NULL); |
| 19 | assert(pLogMessage->GetNext() == NULL); |
| 20 | |
| 21 | if (mpHead == NULL) { |
| 22 | assert(mpTail == NULL); |
| 23 | mpTail = mpHead = pLogMessage; |
| 24 | } else { |
| 25 | assert(mpHead->GetPrev() == NULL); |
| 26 | mpHead->SetPrev(pLogMessage); |
| 27 | pLogMessage->SetNext(mpHead); |
| 28 | mpHead = pLogMessage; |
| 29 | } |
| 30 | |
| 31 | /* update the pool size, and remove old entries if necessary */ |
| 32 | mCurrentSize += pLogMessage->GetFootprint(); |
| 33 | |
| 34 | while (mCurrentSize > mMaxSize) |
| 35 | RemoveOldest(); |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Remove the oldest message (from the tail of the list). |
| 40 | */ |
| 41 | void LogPool::RemoveOldest(void) |
| 42 | { |
| 43 | LogMessage* pPrev; |
| 44 | |
| 45 | if (mpTail == NULL) { |
| 46 | fprintf(stderr, "HEY: nothing left to remove (cur=%ld)\n", |
| 47 | mCurrentSize); |
| 48 | assert(false); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (mpTail == mpBookmark) |
| 53 | mpBookmark = NULL; |
| 54 | |
| 55 | //printf("--- removing oldest, size %ld->%ld (%s)\n", |
| 56 | // mCurrentSize, mCurrentSize - mpTail->GetFootprint(),mpTail->GetMsg()); |
| 57 | mCurrentSize -= mpTail->GetFootprint(); |
| 58 | |
| 59 | pPrev = mpTail->GetPrev(); |
| 60 | mpTail->Release(); |
| 61 | mpTail = pPrev; |
| 62 | if (mpTail == NULL) { |
| 63 | //printf("--- pool is now empty (size=%ld)\n", mCurrentSize); |
| 64 | mpHead = NULL; |
| 65 | } else { |
| 66 | mpTail->SetNext(NULL); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Resize the log pool. |
| 72 | */ |
| 73 | void LogPool::Resize(long maxSize) |
| 74 | { |
| 75 | assert(maxSize >= 0); |
| 76 | |
| 77 | mMaxSize = maxSize; |
| 78 | while (mCurrentSize > mMaxSize) |
| 79 | RemoveOldest(); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Remove all entries. |
| 84 | */ |
| 85 | void LogPool::Clear(void) |
| 86 | { |
| 87 | while (mpTail != NULL) |
| 88 | RemoveOldest(); |
| 89 | } |
| 90 | |