OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTypes_DEFINED | 8 #ifndef SkTypes_DEFINED |
9 #define SkTypes_DEFINED | 9 #define SkTypes_DEFINED |
10 | 10 |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 void* set(void* ptr) { | 507 void* set(void* ptr) { |
508 void* prev = fPtr; | 508 void* prev = fPtr; |
509 fPtr = ptr; | 509 fPtr = ptr; |
510 return prev; | 510 return prev; |
511 } | 511 } |
512 | 512 |
513 /** Transfer ownership of the current ptr to the caller, setting the | 513 /** Transfer ownership of the current ptr to the caller, setting the |
514 internal reference to null. Note the caller is reponsible for calling | 514 internal reference to null. Note the caller is reponsible for calling |
515 sk_free on the returned address. | 515 sk_free on the returned address. |
516 */ | 516 */ |
517 void* detach() { return this->set(NULL); } | 517 void* release() { return this->set(NULL); } |
518 | 518 |
519 /** Free the current buffer, and set the internal reference to NULL. Same | 519 /** Free the current buffer, and set the internal reference to NULL. Same |
520 as calling sk_free(detach()) | 520 as calling sk_free(release()) |
521 */ | 521 */ |
522 void free() { | 522 void free() { |
523 sk_free(fPtr); | 523 sk_free(fPtr); |
524 fPtr = NULL; | 524 fPtr = NULL; |
525 } | 525 } |
526 | 526 |
527 private: | 527 private: |
528 void* fPtr; | 528 void* fPtr; |
529 // illegal | 529 // illegal |
530 SkAutoFree(const SkAutoFree&); | 530 SkAutoFree(const SkAutoFree&); |
531 SkAutoFree& operator=(const SkAutoFree&); | 531 SkAutoFree& operator=(const SkAutoFree&); |
532 }; | 532 }; |
533 #define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree) | 533 #define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree) |
534 | 534 |
535 /** | 535 /** |
536 * Manage an allocated block of heap memory. This object is the sole manager of | 536 * Manage an allocated block of heap memory. This object is the sole manager of |
537 * the lifetime of the block, so the caller must not call sk_free() or delete | 537 * the lifetime of the block, so the caller must not call sk_free() or delete |
538 * on the block, unless detach() was called. | 538 * on the block, unless release() was called. |
539 */ | 539 */ |
540 class SkAutoMalloc : SkNoncopyable { | 540 class SkAutoMalloc : SkNoncopyable { |
541 public: | 541 public: |
542 explicit SkAutoMalloc(size_t size = 0) { | 542 explicit SkAutoMalloc(size_t size = 0) { |
543 fPtr = size ? sk_malloc_throw(size) : NULL; | 543 fPtr = size ? sk_malloc_throw(size) : NULL; |
544 fSize = size; | 544 fSize = size; |
545 } | 545 } |
546 | 546 |
547 ~SkAutoMalloc() { | 547 ~SkAutoMalloc() { |
548 sk_free(fPtr); | 548 sk_free(fPtr); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 /** | 599 /** |
600 * Return the allocated block. | 600 * Return the allocated block. |
601 */ | 601 */ |
602 void* get() { return fPtr; } | 602 void* get() { return fPtr; } |
603 const void* get() const { return fPtr; } | 603 const void* get() const { return fPtr; } |
604 | 604 |
605 /** Transfer ownership of the current ptr to the caller, setting the | 605 /** Transfer ownership of the current ptr to the caller, setting the |
606 internal reference to null. Note the caller is reponsible for calling | 606 internal reference to null. Note the caller is reponsible for calling |
607 sk_free on the returned address. | 607 sk_free on the returned address. |
608 */ | 608 */ |
609 void* detach() { | 609 void* release() { |
610 void* ptr = fPtr; | 610 void* ptr = fPtr; |
611 fPtr = NULL; | 611 fPtr = NULL; |
612 fSize = 0; | 612 fSize = 0; |
613 return ptr; | 613 return ptr; |
614 } | 614 } |
615 | 615 |
616 private: | 616 private: |
617 void* fPtr; | 617 void* fPtr; |
618 size_t fSize; // can be larger than the requested size (see kReuse) | 618 size_t fSize; // can be larger than the requested size (see kReuse) |
619 }; | 619 }; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 | 709 |
710 void* fPtr; | 710 void* fPtr; |
711 size_t fSize; // can be larger than the requested size (see kReuse) | 711 size_t fSize; // can be larger than the requested size (see kReuse) |
712 uint32_t fStorage[kSize >> 2]; | 712 uint32_t fStorage[kSize >> 2]; |
713 }; | 713 }; |
714 // Can't guard the constructor because it's a template class. | 714 // Can't guard the constructor because it's a template class. |
715 | 715 |
716 #endif /* C++ */ | 716 #endif /* C++ */ |
717 | 717 |
718 #endif | 718 #endif |
OLD | NEW |