From 2a912be1d8d27b7aa10daf38b7a5c5251dc9d2ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 6 Apr 2005 00:14:39 +0000 Subject: [PATCH] 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 --- src/kernel/core/util/cbuf.c | 65 +++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/src/kernel/core/util/cbuf.c b/src/kernel/core/util/cbuf.c index c59e689e32..e626fc45b1 100644 --- a/src/kernel/core/util/cbuf.c +++ b/src/kernel/core/util/cbuf.c @@ -1,10 +1,10 @@ /* -** Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. -** Distributed under the terms of the Haiku License. -** -** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. -** Distributed under the terms of the NewOS License. -*/ + * Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. + * Distributed under the terms of the NewOS License. + */ /* This file contains the cbuf functions. Cbuf is a memory allocator, @@ -122,7 +122,6 @@ allocate_cbuf(size_t *_size) static cbuf * allocate_cbuf_mem(size_t size) { - void *_buffer; cbuf *buffer = NULL; cbuf *lastBuffer = NULL; cbuf *headBuffer = NULL; @@ -134,36 +133,33 @@ allocate_cbuf_mem(size_t size) while (size > 0) { foundSize = size; - _buffer = allocate_cbuf(&foundSize); - if (!_buffer) { + buffer = (cbuf *)allocate_cbuf(&foundSize); + if (buffer == NULL) { // couldn't allocate, lets bail with what we have break; } + 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) { - headBuffer = buffer; - headBuffer->flags |= CBUF_FLAG_CHAIN_HEAD; - } + if (headBuffer == NULL) + headBuffer = lastBuffer = buffer; - for (i = 0; i < count; i++) { + while (foundSize > 0) { initialize_cbuf(buffer); - if (lastBuffer) - lastBuffer->next = buffer; - if (headBuffer) - buffer->total_length += buffer->length; + + headBuffer->total_length += buffer->length; + lastBuffer->next = buffer; lastBuffer = buffer; buffer++; + foundSize -= CBUF_LENGTH; } } - if (lastBuffer) { - lastBuffer->next = NULL; + if (headBuffer) { + headBuffer->flags |= CBUF_FLAG_CHAIN_HEAD; lastBuffer->flags |= CBUF_FLAG_CHAIN_TAIL; + lastBuffer->next = NULL; } return headBuffer; @@ -841,32 +837,29 @@ status_t cbuf_truncate_tail(cbuf *buffer, size_t truncBytes) { cbuf *head = buffer; - size_t bufferOffset; size_t offset; if (!buffer || (buffer->flags & CBUF_FLAG_CHAIN_HEAD) == 0) 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; - // ToDo: what happens if truncBytes >= total_length?? - bufferOffset = 0; - while (buffer && offset > 0) { - if (offset < buffer->length) { - // this is the one - bufferOffset = offset; + + while (buffer) { + if (offset < buffer->length) break; - } + offset -= buffer->length; buffer = buffer->next; } if (!buffer) return B_ERROR; - head->total_length -= buffer->length - bufferOffset; - buffer->length -= bufferOffset; - // 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? + head->total_length -= buffer->length - offset; + buffer->length = offset; // clear out the rest of the buffers in this chain while ((buffer = buffer->next) != NULL) {