mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-15 14:25:52 +00:00
Bug 850523 (part 2) - Use C++ comments in LifoAlloc.{cpp,h}. r=wmccloskey.
--HG-- extra : rebase_source : 395321642042559ef2484445fa13c03ba22e8390
This commit is contained in:
parent
9c517940e3
commit
59579d6d63
@ -21,11 +21,9 @@ BumpChunk::new_(size_t chunkSize)
|
||||
return NULL;
|
||||
BumpChunk *result = new (mem) BumpChunk(chunkSize - sizeof(BumpChunk));
|
||||
|
||||
/*
|
||||
* We assume that the alignment of sAlign is less than that of
|
||||
* the underlying memory allocator -- creating a new BumpChunk should
|
||||
* always satisfy the sAlign alignment constraint.
|
||||
*/
|
||||
// We assume that the alignment of sAlign is less than that of
|
||||
// the underlying memory allocator -- creating a new BumpChunk should
|
||||
// always satisfy the sAlign alignment constraint.
|
||||
JS_ASSERT(AlignPtr(result->bump) == result->bump);
|
||||
return result;
|
||||
}
|
||||
@ -51,8 +49,8 @@ BumpChunk::canAlloc(size_t n)
|
||||
return bumped <= limit && bumped > headerBase();
|
||||
}
|
||||
|
||||
} /* namespace detail */
|
||||
} /* namespace js */
|
||||
} // namespace detail
|
||||
} // namespace js
|
||||
|
||||
void
|
||||
LifoAlloc::freeAll()
|
||||
@ -65,10 +63,8 @@ LifoAlloc::freeAll()
|
||||
}
|
||||
first = latest = last = NULL;
|
||||
|
||||
/*
|
||||
* Nb: maintaining curSize_ correctly isn't easy. Fortunately, this is an
|
||||
* excellent sanity check.
|
||||
*/
|
||||
// Nb: maintaining curSize_ correctly isn't easy. Fortunately, this is an
|
||||
// excellent sanity check.
|
||||
JS_ASSERT(curSize_ == 0);
|
||||
}
|
||||
|
||||
@ -76,10 +72,10 @@ LifoAlloc::BumpChunk *
|
||||
LifoAlloc::getOrCreateChunk(size_t n)
|
||||
{
|
||||
if (first) {
|
||||
/* Look for existing, unused BumpChunks to satisfy the request. */
|
||||
// Look for existing, unused BumpChunks to satisfy the request.
|
||||
while (latest->next()) {
|
||||
latest = latest->next();
|
||||
latest->resetBump(); /* This was an unused BumpChunk on the chain. */
|
||||
latest->resetBump(); // This was an unused BumpChunk on the chain.
|
||||
if (latest->canAlloc(n))
|
||||
return latest;
|
||||
}
|
||||
@ -90,7 +86,7 @@ LifoAlloc::getOrCreateChunk(size_t n)
|
||||
if (n > defaultChunkFreeSpace) {
|
||||
size_t allocSizeWithHeader = n + sizeof(BumpChunk);
|
||||
|
||||
/* Guard for overflow. */
|
||||
// Guard for overflow.
|
||||
if (allocSizeWithHeader < n ||
|
||||
(allocSizeWithHeader & (size_t(1) << (tl::BitSize<size_t>::result - 1)))) {
|
||||
return NULL;
|
||||
@ -101,7 +97,7 @@ LifoAlloc::getOrCreateChunk(size_t n)
|
||||
chunkSize = defaultChunkSize_;
|
||||
}
|
||||
|
||||
/* If we get here, we couldn't find an existing BumpChunk to fill the request. */
|
||||
// If we get here, we couldn't find an existing BumpChunk to fill the request.
|
||||
BumpChunk *newChunk = BumpChunk::new_(chunkSize);
|
||||
if (!newChunk)
|
||||
return NULL;
|
||||
|
@ -14,12 +14,10 @@
|
||||
#include "mozilla/MemoryChecking.h"
|
||||
#include "mozilla/TypeTraits.h"
|
||||
|
||||
/*
|
||||
* This data structure supports stacky LIFO allocation (mark/release and
|
||||
* LifoAllocScope). It does not maintain one contiguous segment; instead, it
|
||||
* maintains a bunch of linked memory segments. In order to prevent malloc/free
|
||||
* thrashing, unused segments are deallocated when garbage collection occurs.
|
||||
*/
|
||||
// This data structure supports stacky LIFO allocation (mark/release and
|
||||
// LifoAllocScope). It does not maintain one contiguous segment; instead, it
|
||||
// maintains a bunch of linked memory segments. In order to prevent malloc/free
|
||||
// thrashing, unused segments are deallocated when garbage collection occurs.
|
||||
|
||||
#include "jsutil.h"
|
||||
|
||||
@ -44,13 +42,13 @@ AlignPtr(void *orig)
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Header for a chunk of memory wrangled by the LifoAlloc. */
|
||||
// Header for a chunk of memory wrangled by the LifoAlloc.
|
||||
class BumpChunk
|
||||
{
|
||||
char *bump; /* start of the available data */
|
||||
char *limit; /* end of the data */
|
||||
BumpChunk *next_; /* the next BumpChunk */
|
||||
size_t bumpSpaceSize; /* size of the data area */
|
||||
char *bump; // start of the available data
|
||||
char *limit; // end of the data
|
||||
BumpChunk *next_; // the next BumpChunk
|
||||
size_t bumpSpaceSize; // size of the data area
|
||||
|
||||
char *headerBase() { return reinterpret_cast<char *>(this); }
|
||||
char *bumpBase() const { return limit - bumpSpaceSize; }
|
||||
@ -75,12 +73,12 @@ class BumpChunk
|
||||
#ifdef DEBUG
|
||||
JS_ASSERT(contains(prevBump));
|
||||
|
||||
/* Clobber the now-free space. */
|
||||
// Clobber the now-free space.
|
||||
if (prevBump > bump)
|
||||
memset(bump, 0xcd, prevBump - bump);
|
||||
#endif
|
||||
|
||||
/* Poison/Unpoison memory that we just free'd/allocated */
|
||||
// Poison/Unpoison memory that we just free'd/allocated.
|
||||
#if defined(MOZ_HAVE_MEM_CHECKS)
|
||||
if (prevBump > bump)
|
||||
MOZ_MAKE_MEM_NOACCESS(bump, prevBump - bump);
|
||||
@ -125,7 +123,7 @@ class BumpChunk
|
||||
return limit - AlignPtr(bump);
|
||||
}
|
||||
|
||||
/* Try to perform an allocation of size |n|, return null if not possible. */
|
||||
// Try to perform an allocation of size |n|, return null if not possible.
|
||||
JS_ALWAYS_INLINE
|
||||
void *tryAlloc(size_t n) {
|
||||
char *aligned = AlignPtr(bump);
|
||||
@ -134,11 +132,11 @@ class BumpChunk
|
||||
if (newBump > limit)
|
||||
return NULL;
|
||||
|
||||
/* Check for overflow. */
|
||||
// Check for overflow.
|
||||
if (JS_UNLIKELY(newBump < bump))
|
||||
return NULL;
|
||||
|
||||
JS_ASSERT(canAlloc(n)); /* Ensure consistency between "can" and "try". */
|
||||
JS_ASSERT(canAlloc(n)); // Ensure consistency between "can" and "try".
|
||||
setBump(newBump);
|
||||
return aligned;
|
||||
}
|
||||
@ -153,14 +151,12 @@ class BumpChunk
|
||||
static void delete_(BumpChunk *chunk);
|
||||
};
|
||||
|
||||
} /* namespace detail */
|
||||
} // namespace detail
|
||||
|
||||
/*
|
||||
* LIFO bump allocator: used for phase-oriented and fast LIFO allocations.
|
||||
*
|
||||
* Note: |latest| is not necessary "last". We leave BumpChunks latent in the
|
||||
* chain after they've been released to avoid thrashing before a GC.
|
||||
*/
|
||||
// LIFO bump allocator: used for phase-oriented and fast LIFO allocations.
|
||||
//
|
||||
// Note: |latest| is not necessary "last". We leave BumpChunks latent in the
|
||||
// chain after they've been released to avoid thrashing before a GC.
|
||||
class LifoAlloc
|
||||
{
|
||||
typedef detail::BumpChunk BumpChunk;
|
||||
@ -176,13 +172,11 @@ class LifoAlloc
|
||||
void operator=(const LifoAlloc &) MOZ_DELETE;
|
||||
LifoAlloc(const LifoAlloc &) MOZ_DELETE;
|
||||
|
||||
/*
|
||||
* Return a BumpChunk that can perform an allocation of at least size |n|
|
||||
* and add it to the chain appropriately.
|
||||
*
|
||||
* Side effect: if retval is non-null, |first| and |latest| are initialized
|
||||
* appropriately.
|
||||
*/
|
||||
// Return a BumpChunk that can perform an allocation of at least size |n|
|
||||
// and add it to the chain appropriately.
|
||||
//
|
||||
// Side effect: if retval is non-null, |first| and |latest| are initialized
|
||||
// appropriately.
|
||||
BumpChunk *getOrCreateChunk(size_t n);
|
||||
|
||||
void reset(size_t defaultChunkSize) {
|
||||
@ -219,14 +213,12 @@ class LifoAlloc
|
||||
reset(defaultChunkSize);
|
||||
}
|
||||
|
||||
/* Steal allocated chunks from |other|. */
|
||||
// Steal allocated chunks from |other|.
|
||||
void steal(LifoAlloc *other) {
|
||||
JS_ASSERT(!other->markCount);
|
||||
|
||||
/*
|
||||
* Copy everything from |other| to |this| except for |peakSize_|, which
|
||||
* requires some care.
|
||||
*/
|
||||
// Copy everything from |other| to |this| except for |peakSize_|, which
|
||||
// requires some care.
|
||||
size_t oldPeakSize = peakSize_;
|
||||
PodCopy((char *) this, (char *) other, sizeof(*this));
|
||||
peakSize_ = Max(oldPeakSize, curSize_);
|
||||
@ -234,17 +226,17 @@ class LifoAlloc
|
||||
other->reset(defaultChunkSize_);
|
||||
}
|
||||
|
||||
/* Append all chunks from |other|. They are removed from |other|. */
|
||||
// Append all chunks from |other|. They are removed from |other|.
|
||||
void transferFrom(LifoAlloc *other);
|
||||
|
||||
/* Append unused chunks from |other|. They are removed from |other|. */
|
||||
// Append unused chunks from |other|. They are removed from |other|.
|
||||
void transferUnusedFrom(LifoAlloc *other);
|
||||
|
||||
~LifoAlloc() { freeAll(); }
|
||||
|
||||
size_t defaultChunkSize() const { return defaultChunkSize_; }
|
||||
|
||||
/* Frees all held memory. */
|
||||
// Frees all held memory.
|
||||
void freeAll();
|
||||
|
||||
JS_ALWAYS_INLINE
|
||||
@ -301,10 +293,8 @@ class LifoAlloc
|
||||
return (T *) mem;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create an array with uninitialized elements of type |T|.
|
||||
* The caller is responsible for initialization.
|
||||
*/
|
||||
// Create an array with uninitialized elements of type |T|.
|
||||
// The caller is responsible for initialization.
|
||||
template <typename T>
|
||||
T *newArrayUninitialized(size_t count) {
|
||||
return static_cast<T *>(alloc(sizeof(T) * count));
|
||||
@ -326,11 +316,9 @@ class LifoAlloc
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the chunk that contains |mark|, and make sure we don't pass
|
||||
* |latest| along the way -- we should be making the chain of active
|
||||
* chunks shorter, not longer!
|
||||
*/
|
||||
// Find the chunk that contains |mark|, and make sure we don't pass
|
||||
// |latest| along the way -- we should be making the chain of active
|
||||
// chunks shorter, not longer!
|
||||
BumpChunk *container;
|
||||
for (container = first; !container->contains(mark); container = container->next())
|
||||
JS_ASSERT(container != latest);
|
||||
@ -346,7 +334,7 @@ class LifoAlloc
|
||||
latest->resetBump();
|
||||
}
|
||||
|
||||
/* Get the total "used" (occupied bytes) count for the arena chunks. */
|
||||
// Get the total "used" (occupied bytes) count for the arena chunks.
|
||||
size_t used() const {
|
||||
size_t accum = 0;
|
||||
for (BumpChunk *chunk = first; chunk; chunk = chunk->next()) {
|
||||
@ -357,29 +345,27 @@ class LifoAlloc
|
||||
return accum;
|
||||
}
|
||||
|
||||
/* Get the total size of the arena chunks (including unused space). */
|
||||
// Get the total size of the arena chunks (including unused space).
|
||||
size_t sizeOfExcludingThis(JSMallocSizeOfFun mallocSizeOf) const {
|
||||
size_t n = 0;
|
||||
for (BumpChunk *chunk = first; chunk; chunk = chunk->next())
|
||||
n += chunk->sizeOfIncludingThis(mallocSizeOf);
|
||||
|
||||
/* While we're here, let's sanity check curSize_. */
|
||||
// While we're here, let's sanity check curSize_.
|
||||
MOZ_ASSERT(curSize_ == n);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* Like sizeOfExcludingThis(), but includes the size of the LifoAlloc itself. */
|
||||
// Like sizeOfExcludingThis(), but includes the size of the LifoAlloc itself.
|
||||
size_t sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf) const {
|
||||
return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the peak size of the arena chunks (including unused space and
|
||||
* bookkeeping space).
|
||||
*/
|
||||
// Get the peak size of the arena chunks (including unused space and
|
||||
// bookkeeping space).
|
||||
size_t peakSizeOfExcludingThis() const { return peakSize_; }
|
||||
|
||||
/* Doesn't perform construction; useful for lazily-initialized POD types. */
|
||||
// Doesn't perform construction; useful for lazily-initialized POD types.
|
||||
template <typename T>
|
||||
JS_ALWAYS_INLINE
|
||||
T *newPod() {
|
||||
@ -421,6 +407,6 @@ class LifoAllocScope
|
||||
}
|
||||
};
|
||||
|
||||
} /* namespace js */
|
||||
} // namespace js
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user