Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fb2f3a53 authored by Kenny Root's avatar Kenny Root Committed by Android (Google) Code Review
Browse files

Merge "Use rand() for MinGW"

parents 0a4d51d5 e04f826f
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -82,6 +82,9 @@ private:
    BlobCache(const BlobCache&);
    BlobCache(const BlobCache&);
    void operator=(const BlobCache&);
    void operator=(const BlobCache&);


    // A random function helper to get around MinGW not having nrand48()
    long int blob_random();

    // clean evicts a randomly chosen set of entries from the cache such that
    // clean evicts a randomly chosen set of entries from the cache such that
    // the total size of all remaining entries is less than mMaxTotalSize/2.
    // the total size of all remaining entries is less than mMaxTotalSize/2.
    void clean();
    void clean();
+13 −1
Original line number Original line Diff line number Diff line
@@ -31,9 +31,13 @@ BlobCache::BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize
        mMaxTotalSize(maxTotalSize),
        mMaxTotalSize(maxTotalSize),
        mTotalSize(0) {
        mTotalSize(0) {
    nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
    nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
#ifdef _WIN32
    srand(now);
#else
    mRandState[0] = (now >> 0) & 0xFFFF;
    mRandState[0] = (now >> 0) & 0xFFFF;
    mRandState[1] = (now >> 16) & 0xFFFF;
    mRandState[1] = (now >> 16) & 0xFFFF;
    mRandState[2] = (now >> 32) & 0xFFFF;
    mRandState[2] = (now >> 32) & 0xFFFF;
#endif
    LOGV("initializing random seed using %lld", now);
    LOGV("initializing random seed using %lld", now);
}
}


@@ -148,11 +152,19 @@ size_t BlobCache::get(const void* key, size_t keySize, void* value,
    return valueBlobSize;
    return valueBlobSize;
}
}


long int BlobCache::blob_random() {
#ifdef _WIN32
    return rand();
#else
    return nrand48(mRandState);
#endif
}

void BlobCache::clean() {
void BlobCache::clean() {
    // Remove a random cache entry until the total cache size gets below half
    // Remove a random cache entry until the total cache size gets below half
    // the maximum total cache size.
    // the maximum total cache size.
    while (mTotalSize > mMaxTotalSize / 2) {
    while (mTotalSize > mMaxTotalSize / 2) {
        size_t i = size_t(nrand48(mRandState) % (mCacheEntries.size()));
        size_t i = size_t(blob_random() % (mCacheEntries.size()));
        const CacheEntry& entry(mCacheEntries[i]);
        const CacheEntry& entry(mCacheEntries[i]);
        mTotalSize -= entry.getKey()->getSize() + entry.getValue()->getSize();
        mTotalSize -= entry.getKey()->getSize() + entry.getValue()->getSize();
        mCacheEntries.removeAt(i);
        mCacheEntries.removeAt(i);