bool nsAttrAndChildArray::GrowBy(PRUint32 aGrowSize) { PRUint32 size = mImpl ? mImpl->mBufferSize + NS_IMPL_EXTRA_SIZE : 0; PRUint32 minSize = size + aGrowSize; if (minSize <= ATTRCHILD_ARRAY_LINEAR_THRESHOLD) { do { size += ATTRCHILD_ARRAY_GROWSIZE; } while (size < minSize); } else { size = PR_BIT(PR_CeilingLog2(minSize)); } bool needToInitialize = !mImpl; Impl* newImpl = static_cast<Impl*>(PR_Realloc(mImpl, size * sizeof(void*))); NS_ENSURE_TRUE(newImpl, false); mImpl = newImpl; // Set initial counts if we didn't have a buffer before if (needToInitialize) { mImpl->mMappedAttrs = nsnull; SetAttrSlotAndChildCount(0, 0); } mImpl->mBufferSize = size - NS_IMPL_EXTRA_SIZE; return true; }
PRBool nsVoidArray::GrowArrayBy(PRInt32 aGrowBy) { // We have to grow the array. Grow by kMinGrowArrayBy slots if we're // smaller than kLinearThreshold bytes, or a power of two if we're // larger. This is much more efficient with most memory allocators, // especially if it's very large, or of the allocator is binned. if (aGrowBy < kMinGrowArrayBy) aGrowBy = kMinGrowArrayBy; PRUint32 newCapacity = GetArraySize() + aGrowBy; // Minimum increase PRUint32 newSize = SIZEOF_IMPL(newCapacity); if (newSize >= (PRUint32) kLinearThreshold) { // newCount includes enough space for at least kMinGrowArrayBy new // slots. Select the next power-of-two size in bytes above or // equal to that. // Also, limit the increase in size to about a VM page or two. if (GetArraySize() >= kMaxGrowArrayBy) { newCapacity = GetArraySize() + PR_MAX(kMaxGrowArrayBy,aGrowBy); newSize = SIZEOF_IMPL(newCapacity); } else { PR_CEILING_LOG2(newSize, newSize); newCapacity = CAPACITYOF_IMPL(PR_BIT(newSize)); } } // frees old mImpl IF this succeeds if (!SizeTo(newCapacity)) return PR_FALSE; return PR_TRUE; }
PRBool nsSupportsArray::GrowArrayBy(PRInt32 aGrowBy) { // We have to grow the array. Grow by kGrowArrayBy slots if we're smaller // than kLinearThreshold bytes, or a power of two if we're larger. // This is much more efficient with most memory allocators, especially // if it's very large, or of the allocator is binned. if (aGrowBy < kGrowArrayBy) aGrowBy = kGrowArrayBy; PRUint32 newCount = mArraySize + aGrowBy; // Minimum increase PRUint32 newSize = sizeof(mArray[0]) * newCount; if (newSize >= (PRUint32) kLinearThreshold) { // newCount includes enough space for at least kGrowArrayBy new slots. // Select the next power-of-two size in bytes above that if newSize is // not a power of two. if (newSize & (newSize - 1)) newSize = PR_BIT(PR_CeilingLog2(newSize)); newCount = newSize / sizeof(mArray[0]); } // XXX This would be far more efficient in many allocators if we used // XXX PR_Realloc(), etc nsISupports** oldArray = mArray; mArray = new nsISupports*[newCount]; if (!mArray) { // ran out of memory mArray = oldArray; return PR_FALSE; } mArraySize = newCount; #if DEBUG_SUPPORTSARRAY if (oldArray == mArray) // can't happen without use of realloc ADD_TO_STATS(GrowInPlace,mCount); ADD_TO_STATS(AllocedOfSize,mArraySize*sizeof(mArray[0])); if (mArraySize > mMaxSize) { ADD_TO_STATS(NumberOfSize,mArraySize*sizeof(mArray[0])); if (oldArray != &(mAutoArray[0])) SUB_FROM_STATS(NumberOfSize,mCount*sizeof(mArray[0])); mMaxSize = mArraySize; } #endif if (oldArray) { // need to move old data if (0 < mCount) { ::memcpy(mArray, oldArray, mCount * sizeof(nsISupports*)); } if (oldArray != &(mAutoArray[0])) { delete[] oldArray; } } return PR_TRUE; }
event_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { JSEvent *event; jsint slot, i; JSString *str; const char *name; uint32 temp; if (!JSVAL_IS_INT(id)) return JS_TRUE; event = JS_GetInstancePrivate(cx, obj, &lm_event_class, NULL); if (!event) return JS_TRUE; slot = JSVAL_TO_INT(id); if (slot==EVENT_TYPE) { if (!JSVAL_IS_STRING(*vp) || !(str = JS_ValueToString(cx, *vp))) return JS_FALSE; name = JS_GetStringBytes(str); for (i = 0; i < NUM_EVENTS; i++) { if (!XP_STRCASECMP(event_names[i].lowerName, name)) event->type = PR_BIT(i); } } else if ((slot==EVENT_X) || (slot==EVENT_LAYERX)) return JS_ValueToInt32(cx, *vp, &event->x); else if ((slot==EVENT_Y) || (slot==EVENT_LAYERY)) return JS_ValueToInt32(cx, *vp, &event->y); else if (slot==EVENT_DOCX) return JS_ValueToInt32(cx, *vp, &event->docx); else if (slot==EVENT_DOCY) return JS_ValueToInt32(cx, *vp, &event->docy); else if (slot==EVENT_SCREENX) return JS_ValueToInt32(cx, *vp, &event->screenx); else if (slot==EVENT_SCREENY) return JS_ValueToInt32(cx, *vp, &event->screeny); else if (slot==EVENT_WHICH) return GetUint32(cx, *vp, &event->which); else if (slot==EVENT_MODIFIERS) GetUint32(cx, *vp, &temp); event->modifiers = temp; return JS_TRUE; /* Win16 hack */ /*else if (slot==EVENT_DATA) { GetUint32(cx, *vp, &temp); event->data = temp; return JS_TRUE; } */ return JS_TRUE; }