Differences From Artifact [1c8a76eb56]:
- File
src/OFDataArray.m
— part of check-in
[f7576a66ce]
at
2012-06-06 13:47:52
on branch trunk
— Slightly change the memory management API.
Also fix a bug where OFBigDataArray would waste memory. (user: js, size: 12290) [annotate] [blame] [check-ins using] [more...]
To Artifact [a3f3ece2b2]:
- File src/OFDataArray.m — part of check-in [11d3d69a22] at 2012-06-10 13:28:05 on branch trunk — More API improvements. (user: js, size: 12284) [annotate] [blame] [check-ins using] [more...]
︙ | ︙ | |||
107 108 109 110 111 112 113 | @try { char *buffer = [self allocMemoryWithSize: of_pagesize]; while (![file isAtEndOfStream]) { size_t length; | | | | 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | @try { char *buffer = [self allocMemoryWithSize: of_pagesize]; while (![file isAtEndOfStream]) { size_t length; length = [file readIntoBuffer: buffer length: of_pagesize]; [self addItemsFromCArray: buffer count: length]; } [self freeMemory: buffer]; } @finally { [file release]; |
︙ | ︙ | |||
253 254 255 256 257 258 259 | - (void)addItem: (const void*)item { if (SIZE_MAX - count < 1) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data | | | 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | - (void)addItem: (const void*)item { if (SIZE_MAX - count < 1) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data size: itemSize count: count + 1]; memcpy(data + count * itemSize, item, itemSize); count++; } |
︙ | ︙ | |||
276 277 278 279 280 281 282 | - (void)addItemsFromCArray: (const void*)cArray count: (size_t)nItems { if (nItems > SIZE_MAX - count) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data | | | | 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | - (void)addItemsFromCArray: (const void*)cArray count: (size_t)nItems { if (nItems > SIZE_MAX - count) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data size: itemSize count: count + nItems]; memcpy(data + count * itemSize, cArray, nItems * itemSize); count += nItems; } - (void)insertItemsFromCArray: (const void*)cArray atIndex: (size_t)index count: (size_t)nItems { if (nItems > SIZE_MAX - count || index > count) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data size: itemSize count: count + nItems]; memmove(data + (index + nItems) * itemSize, data + index * itemSize, (count - index) * itemSize); memcpy(data + index * itemSize, cArray, nItems * itemSize); count += nItems; |
︙ | ︙ | |||
318 319 320 321 322 323 324 | memmove(data + range.start * itemSize, data + (range.start + range.length) * itemSize, (count - range.start - range.length) * itemSize); count -= range.length; @try { data = [self resizeMemory: data | | | | 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | memmove(data + range.start * itemSize, data + (range.start + range.length) * itemSize, (count - range.start - range.length) * itemSize); count -= range.length; @try { data = [self resizeMemory: data size: itemSize count: count]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } - (void)removeLastItem { if (count < 1) @throw [OFOutOfRangeException exceptionWithClass: isa]; count--; @try { data = [self resizeMemory: data size: itemSize count: count]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only made it smaller */ } } - (void)removeAllItems |
︙ | ︙ | |||
433 434 435 436 437 438 439 | - (void)writeToFile: (OFString*)path { OFFile *file = [[OFFile alloc] initWithPath: path mode: @"wb"]; @try { | < | > | 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | - (void)writeToFile: (OFString*)path { OFFile *file = [[OFFile alloc] initWithPath: path mode: @"wb"]; @try { [file writeBuffer: data length: count * itemSize]; } @finally { [file release]; } } - (OFXMLElement*)XMLElementBySerializing { |
︙ | ︙ | |||
476 477 478 479 480 481 482 | @throw [OFOutOfRangeException exceptionWithClass: isa]; lastPageByte = of_pagesize - 1; newSize = ((count + 1) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data | | | | 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | @throw [OFOutOfRangeException exceptionWithClass: isa]; lastPageByte = of_pagesize - 1; newSize = ((count + 1) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data size: newSize]; memcpy(data + count * itemSize, item, itemSize); count++; size = newSize; } - (void)addItemsFromCArray: (const void*)cArray count: (size_t)nItems { size_t newSize, lastPageByte; if (nItems > SIZE_MAX - count || count + nItems > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: isa]; lastPageByte = of_pagesize - 1; newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data size: newSize]; memcpy(data + count * itemSize, cArray, nItems * itemSize); count += nItems; size = newSize; } |
︙ | ︙ | |||
520 521 522 523 524 525 526 | @throw [OFOutOfRangeException exceptionWithClass: isa]; lastPageByte = of_pagesize - 1; newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data | | | 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | @throw [OFOutOfRangeException exceptionWithClass: isa]; lastPageByte = of_pagesize - 1; newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data size: newSize]; memmove(data + (index + nItems) * itemSize, data + index * itemSize, (count - index) * itemSize); memcpy(data + index * itemSize, cArray, nItems * itemSize); count += nItems; size = newSize; |
︙ | ︙ | |||
547 548 549 550 551 552 553 | count -= range.length; lastPageByte = of_pagesize - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data | | | | 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | count -= range.length; lastPageByte = of_pagesize - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data size: newSize]; size = newSize; } - (void)removeLastItem { size_t newSize, lastPageByte; if (count < 1) @throw [OFOutOfRangeException exceptionWithClass: isa]; count--; lastPageByte = of_pagesize - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) { @try { data = [self resizeMemory: data size: newSize]; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only made it smaller */ } size = newSize; } } |
︙ | ︙ |