Adapted Michael Noisternig's recent changes to the NewOS cbuf implementation

to ours - pretty hefty bugs, I wonder how it could work before at all.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12261 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-04-06 00:14:39 +00:00
parent 30cf0784e9
commit 2a912be1d8

View File

@ -1,10 +1,10 @@
/* /*
** Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. * Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the Haiku License. * Distributed under the terms of the MIT License.
** *
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License. * Distributed under the terms of the NewOS License.
*/ */
/* This file contains the cbuf functions. Cbuf is a memory allocator, /* This file contains the cbuf functions. Cbuf is a memory allocator,
@ -122,7 +122,6 @@ allocate_cbuf(size_t *_size)
static cbuf * static cbuf *
allocate_cbuf_mem(size_t size) allocate_cbuf_mem(size_t size)
{ {
void *_buffer;
cbuf *buffer = NULL; cbuf *buffer = NULL;
cbuf *lastBuffer = NULL; cbuf *lastBuffer = NULL;
cbuf *headBuffer = NULL; cbuf *headBuffer = NULL;
@ -134,36 +133,33 @@ allocate_cbuf_mem(size_t size)
while (size > 0) { while (size > 0) {
foundSize = size; foundSize = size;
_buffer = allocate_cbuf(&foundSize); buffer = (cbuf *)allocate_cbuf(&foundSize);
if (!_buffer) { if (buffer == NULL) {
// couldn't allocate, lets bail with what we have // couldn't allocate, lets bail with what we have
break; break;
} }
size -= foundSize; size -= foundSize;
count = foundSize / CBUF_LENGTH;
// dprintf("allocate_cbuf_mem: returned %d of memory, %d left\n", found_size, size);
buffer = (cbuf *)_buffer; if (headBuffer == NULL)
if (headBuffer == NULL) { headBuffer = lastBuffer = buffer;
headBuffer = buffer;
headBuffer->flags |= CBUF_FLAG_CHAIN_HEAD;
}
for (i = 0; i < count; i++) { while (foundSize > 0) {
initialize_cbuf(buffer); initialize_cbuf(buffer);
if (lastBuffer)
headBuffer->total_length += buffer->length;
lastBuffer->next = buffer; lastBuffer->next = buffer;
if (headBuffer)
buffer->total_length += buffer->length;
lastBuffer = buffer; lastBuffer = buffer;
buffer++; buffer++;
foundSize -= CBUF_LENGTH;
} }
} }
if (lastBuffer) { if (headBuffer) {
lastBuffer->next = NULL; headBuffer->flags |= CBUF_FLAG_CHAIN_HEAD;
lastBuffer->flags |= CBUF_FLAG_CHAIN_TAIL; lastBuffer->flags |= CBUF_FLAG_CHAIN_TAIL;
lastBuffer->next = NULL;
} }
return headBuffer; return headBuffer;
@ -841,32 +837,29 @@ status_t
cbuf_truncate_tail(cbuf *buffer, size_t truncBytes) cbuf_truncate_tail(cbuf *buffer, size_t truncBytes)
{ {
cbuf *head = buffer; cbuf *head = buffer;
size_t bufferOffset;
size_t offset; size_t offset;
if (!buffer || (buffer->flags & CBUF_FLAG_CHAIN_HEAD) == 0) if (!buffer || (buffer->flags & CBUF_FLAG_CHAIN_HEAD) == 0)
return B_BAD_VALUE; return B_BAD_VALUE;
// we can't remove more than there is
if (truncBytes > head->total_length)
truncBytes = head->total_length;
offset = buffer->total_length - truncBytes; offset = buffer->total_length - truncBytes;
// ToDo: what happens if truncBytes >= total_length??
bufferOffset = 0; while (buffer) {
while (buffer && offset > 0) { if (offset < buffer->length)
if (offset < buffer->length) {
// this is the one
bufferOffset = offset;
break; break;
}
offset -= buffer->length; offset -= buffer->length;
buffer = buffer->next; buffer = buffer->next;
} }
if (!buffer) if (!buffer)
return B_ERROR; return B_ERROR;
head->total_length -= buffer->length - bufferOffset; head->total_length -= buffer->length - offset;
buffer->length -= bufferOffset; buffer->length = offset;
// ToDo: is this correct?
// Isn't bufferOffset the part that has to stay in the buffer?
// can't we just have head->total_length -= truncBytes?
// clear out the rest of the buffers in this chain // clear out the rest of the buffers in this chain
while ((buffer = buffer->next) != NULL) { while ((buffer = buffer->next) != NULL) {