Skip to content

Commit 87be8c4

Browse files
Allow cursor window to grow memory allocation on demand
1 parent 33e3d96 commit 87be8c4

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

jni/CursorWindow.cpp

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ CursorWindow::CursorWindow(size_t maxSize) :
4343
// mData = (uint8_t *) memory->pointer();
4444
// if (mData == NULL) {
4545
// return false;
46+
4647
// }
4748
// mHeader = (window_header_t *) mData;
4849

@@ -121,39 +122,27 @@ LOG_WINDOW("Allocated row %u, rowSlot is at offset %u, fieldDir is %d bytes at o
121122

122123
uint32_t CursorWindow::alloc(size_t requestedSize, bool aligned)
123124
{
124-
int32_t size;
125+
size_t size = 0, new_allocation_sz = 0;
125126
uint32_t padding;
127+
void *tempData = NULL;
126128
if (aligned) {
127129
// 4 byte alignment
128130
padding = 4 - (mFreeOffset & 0x3);
129131
} else {
130132
padding = 0;
131133
}
132-
133134
size = requestedSize + padding;
134-
135135
if (size > freeSpace()) {
136-
LOGE("need to grow: mSize = %d, size = %d, freeSpace() = %d, numRows = %d", mSize, size, freeSpace(), mHeader->numRows);
137-
// Only grow the window if the first row doesn't fit
138-
if (mHeader->numRows > 1) {
139-
LOGE("not growing since there are already %d row(s), max size %d", mHeader->numRows, mMaxSize);
140-
return 0;
141-
}
142-
143-
// Find a new size that will fit the allocation
144-
int allocated = mSize - freeSpace();
145-
int newSize = mSize + WINDOW_ALLOCATION_SIZE;
146-
while (size > (newSize - allocated)) {
147-
newSize += WINDOW_ALLOCATION_SIZE;
148-
if (newSize > mMaxSize) {
149-
LOGE("Attempting to grow window beyond max size (%d)", mMaxSize);
150-
return 0;
151-
}
152-
}
153-
LOG_WINDOW("found size %d", newSize);
154-
mSize = newSize;
136+
LOGE("need to grow: mSize = %d, size = %d, freeSpace() = %d, numRows = %d",
137+
mSize, size, freeSpace(), mHeader->numRows);
138+
new_allocation_sz = mSize + size - freeSpace();
139+
tempData = realloc((void *)mData, new_allocation_sz);
140+
if(tempData == NULL) return 0;
141+
mData = (uint8_t *)tempData;
142+
mHeader = (window_header_t *)mData;
143+
LOGE("allocation grew to:%d", new_allocation_sz);
144+
mSize = new_allocation_sz;
155145
}
156-
157146
uint32_t offset = mFreeOffset + padding;
158147
mFreeOffset += size;
159148
return offset;

0 commit comments

Comments
 (0)