1 Subject: Remove malloc overrides
2 Last-Update: 2012-03-31
3 Origin, upstream, http://code.google.com/p/nvidia-texture-tools/source/detail?r=1172
4 Bug: http://code.google.com/p/nvidia-texture-tools/issues/detail?id=138
6 diff --git a/src/nvcore/Containers.h b/src/nvcore/Containers.h
7 index f0b63d4..3d1d8ee 100644
8 --- a/src/nvcore/Containers.h
9 +++ b/src/nvcore/Containers.h
10 @@ -16,9 +16,9 @@ Do not use memmove in insert & remove, use copy ctors instead.
14 -#include <nvcore/nvcore.h>
15 -#include <nvcore/Memory.h>
16 -#include <nvcore/Debug.h>
21 #include <string.h> // memmove
22 #include <new> // for placement new
23 @@ -589,15 +589,15 @@ namespace nv
25 if( m_buffer_size == 0 ) {
27 - mem::free( m_buffer );
35 - if( m_buffer ) m_buffer = (T *) mem::realloc( m_buffer, sizeof(T) * m_buffer_size );
36 - else m_buffer = (T *) mem::malloc( sizeof(T) * m_buffer_size );
37 + if( m_buffer ) m_buffer = (T *) realloc(m_buffer, sizeof(T) * m_buffer_size);
38 + else m_buffer = (T *) ::malloc(sizeof(T) * m_buffer_size);
42 @@ -778,7 +778,7 @@ namespace nv
51 @@ -1001,7 +1001,7 @@ namespace nv
52 new_size = nextPowerOfTwo(new_size);
54 HashMap<T, U, hash_functor> new_hash;
55 - new_hash.table = (Entry *) mem::malloc(sizeof(Entry) * new_size);
56 + new_hash.table = (Entry *) ::malloc(sizeof(Entry) * new_size);
57 nvDebugCheck(new_hash.table != NULL);
59 new_hash.entry_count = 0;
60 @@ -1026,7 +1026,7 @@ namespace nv
63 // Delete our old data buffer.
68 // Steal new_hash's data.
69 diff --git a/src/nvcore/Memory.cpp b/src/nvcore/Memory.cpp
70 index 7ece018..d470580 100644
71 --- a/src/nvcore/Memory.cpp
72 +++ b/src/nvcore/Memory.cpp
74 +// This code is in the public domain -- Ignacio CastaƱo <castano@gmail.com>
80 -//#include <malloc.h>
88 +extern "C" void *EF_malloc(size_t size);
89 +extern "C" void *EF_realloc(void * oldBuffer, size_t newSize);
90 +extern "C" void EF_free(void * address);
95 -void * nv::mem::malloc(size_t size)
96 +#if NV_OVERRIDE_ALLOC
98 +void * malloc(size_t size)
101 + return EF_malloc(size);
103 + return ::malloc(size);
107 +void * debug_malloc(size_t size, const char * file, int line)
112 + return EF_malloc(size);
114 + return ::malloc(size);
118 +void free(void * ptr)
120 - return ::malloc(size);
122 + return EF_free(const_cast<void *>(ptr));
124 + ::free(const_cast<void *>(ptr));
128 -void * nv::mem::malloc(size_t size, const char * file, int line)
129 +void * realloc(void * ptr, size_t size)
133 - return ::malloc(size);
134 + nvDebugCheck(ptr != NULL || size != 0); // undefined realloc behavior.
136 + return EF_realloc(ptr, size);
138 + return ::realloc(ptr, size);
142 -void nv::mem::free(const void * ptr)
143 +/* No need to override this unless we want line info.
144 +void * operator new (size_t size) throw()
146 - ::free(const_cast<void *>(ptr));
147 + return malloc(size);
150 -void * nv::mem::realloc(void * ptr, size_t size)
151 +void operator delete (void *p) throw()
153 - nvDebugCheck(ptr != NULL || size != 0); // undefined realloc behavior.
154 - return ::realloc(ptr, size);
158 +void * operator new [] (size_t size) throw()
160 + return malloc(size);
163 +void operator delete [] (void * p) throw()
169 +#if 0 // Code from Apple:
170 +void* operator new(std::size_t sz) throw (std::bad_alloc)
172 + void *result = std::malloc (sz == 0 ? 1 : sz);
173 + if (result == NULL)
174 + throw std::bad_alloc();
178 +void operator delete(void* p) throw()
186 +/* These are the 'nothrow' versions of the above operators.
187 + The system version will try to call a std::new_handler if they
188 + fail, but your overriding versions are not required to do this. */
189 +void* operator new(std::size_t sz, const std::nothrow_t&) throw()
192 + void * result = ::operator new (sz); // calls our overridden operator new
194 + } catch (std::bad_alloc &) {
198 +void operator delete(void* p, const std::nothrow_t&) throw()
200 + ::operator delete (p);
206 +#endif // NV_OVERRIDE_ALLOC
207 diff --git a/src/nvcore/Memory.h b/src/nvcore/Memory.h
208 index d699926..897388b 100644
209 --- a/src/nvcore/Memory.h
210 +++ b/src/nvcore/Memory.h
212 -// This code is in the public domain -- castanyo@yahoo.es
213 +// This code is in the public domain -- Ignacio CastaƱo <castano@gmail.com>
216 #ifndef NV_CORE_MEMORY_H
217 #define NV_CORE_MEMORY_H
219 -#include <nvcore/nvcore.h>
222 #include <stdlib.h> // malloc(), realloc() and free()
223 -#include <stddef.h> // size_t
224 +#include <stddef.h> // size_t
226 #include <new> // new and delete
228 -// Custom memory allocator
233 - NVCORE_API void * malloc(size_t size);
234 - NVCORE_API void * malloc(size_t size, const char * file, int line);
236 - NVCORE_API void free(const void * ptr);
237 - NVCORE_API void * realloc(void * ptr, size_t size);
244 -// Override new/delete
245 +#define NV_OVERRIDE_ALLOC 0
247 -inline void * operator new (size_t size) throw()
249 - return nv::mem::malloc(size);
252 -inline void operator delete (void *p) throw()
256 +#if NV_OVERRIDE_ALLOC
258 -inline void * operator new [] (size_t size) throw()
260 - return nv::mem::malloc(size);
263 -inline void operator delete [] (void * p) throw()
266 +// Custom memory allocator
268 + NVCORE_API void * malloc(size_t size);
269 + NVCORE_API void * debug_malloc(size_t size, const char * file, int line);
270 + NVCORE_API void free(void * ptr);
271 + NVCORE_API void * realloc(void * ptr, size_t size);
276 #define new new(__FILE__, __LINE__)
277 -#define malloc(i) malloc(i, __FILE__, __LINE__)
278 +#define malloc(i) debug_malloc(i, __FILE__, __LINE__)
288 - Abstract: Overrides the C++ 'operator new' and 'operator delete'.
290 - Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
291 - ("Apple") in consideration of your agreement to the following terms, and your
292 - use, installation, modification or redistribution of this Apple software
293 - constitutes acceptance of these terms. If you do not agree with these terms,
294 - please do not use, install, modify or redistribute this Apple software.
296 - In consideration of your agreement to abide by the following terms, and subject
297 - to these terms, Apple grants you a personal, non-exclusive license, under Appleās
298 - copyrights in this original Apple software (the "Apple Software"), to use,
299 - reproduce, modify and redistribute the Apple Software, with or without
300 - modifications, in source and/or binary forms; provided that if you redistribute
301 - the Apple Software in its entirety and without modifications, you must retain
302 - this notice and the following text and disclaimers in all such redistributions of
303 - the Apple Software. Neither the name, trademarks, service marks or logos of
304 - Apple Computer, Inc. may be used to endorse or promote products derived from the
305 - Apple Software without specific prior written permission from Apple. Except as
306 - expressly stated in this notice, no other rights or licenses, express or implied,
307 - are granted by Apple herein, including but not limited to any patent rights that
308 - may be infringed by your derivative works or by other works in which the Apple
309 - Software may be incorporated.
311 - The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
312 - WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
313 - WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
314 - PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
315 - COMBINATION WITH YOUR PRODUCTS.
317 - IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
318 - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
319 - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
320 - ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
321 - OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
322 - (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
323 - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325 - Copyright Ā© 2006 Apple Computer, Inc., All Rights Reserved
328 -/* This sample shows how to override the C++ global 'new' and 'delete' operators. */
332 -#include <stdexcept>
335 -/* Some variables and code to make the example do something. */
337 - unsigned long long gNewCounter; // number of times 'new' was called
338 - unsigned long long gDeleteCounter; // number of times 'delete' was called
340 - void printCounters() // print the counters above
342 - std::cout << "new was called " << gNewCounter << " times and delete was called " << gDeleteCounter << " times\n";
346 -/* These are the overridden new and delete routines.
347 - Most applications will want to override at least these four versions of new/delete if they override any of them.
349 - In Mac OS, it's not necessary to override the array versions of operator new and delete if all
350 - they would do is call the non-array versions; the C++ standard library, as an extension
351 - to the C++ standard, does this for you.
354 - Developers should consult the section [lib.support.dynamic] in the C++ standard to see the requirements
355 - on the generic operators new and delete; the system may expect that your overridden operators meet all these
359 - Your operators may be called by the system, even early in start-up before constructors have been executed. */
360 -void* operator new(std::size_t sz) throw (std::bad_alloc)
362 - void *result = std::malloc (sz == 0 ? 1 : sz);
363 - if (result == NULL)
364 - throw std::bad_alloc();
368 -void operator delete(void* p) throw()
376 + template <typename T> T * malloc(size_t count) {
377 + return (T *)::malloc(sizeof(T) * count);
380 -/* These are the 'nothrow' versions of the above operators.
381 - The system version will try to call a std::new_handler if they
382 - fail, but your overriding versions are not required to do this. */
383 -void* operator new(std::size_t sz, const std::nothrow_t&) throw()
386 - void * result = ::operator new (sz); // calls our overridden operator new
388 - } catch (std::bad_alloc &) {
392 -void operator delete(void* p, const std::nothrow_t&) throw()
394 - ::operator delete (p);
396 + template <typename T> T * realloc(T * ptr, size_t count) {
397 + return (T *)::realloc(ptr, sizeof(T) * count);
400 -/* Bug 4067110 is that if your program has no weak symbols at all, the linker will not set the
401 - WEAK_DEFINES bit in the Mach-O header and as a result the new and delete operators above won't
402 - be seen by system libraries. This is mostly a problem for test programs and small examples,
403 - since almost all real C++ programs complicated enough to override new and delete will have at
404 - least one weak symbol. However, this is a small example, so: */
405 -void __attribute__((weak, visibility("default"))) workaroundFor4067110 () { }
406 + template <typename T> void free(const T * ptr) {
407 + ::free((void *)ptr);
410 -/* This is a simple test program that causes the runtime library to call new and delete. */
413 - atexit (printCounters);
415 - std::locale example("does_not_exist");
416 - } catch (std::runtime_error &x) {
423 #endif // NV_CORE_MEMORY_H
424 diff --git a/src/nvcore/StrLib.cpp b/src/nvcore/StrLib.cpp
425 index 34cf992..34518d9 100644
426 --- a/src/nvcore/StrLib.cpp
427 +++ b/src/nvcore/StrLib.cpp
428 @@ -21,17 +21,17 @@ namespace
430 static char * strAlloc(uint size)
432 - return static_cast<char *>(mem::malloc(size));
433 + return static_cast<char *>(::malloc(size));
436 static char * strReAlloc(char * str, uint size)
438 - return static_cast<char *>(mem::realloc(str, size));
439 + return static_cast<char *>(::realloc(str, size));
442 static void strFree(const char * str)
444 - return mem::free(const_cast<char *>(str));
445 + return ::free(const_cast<char *>(str));
448 /*static char * strDup( const char * str )
449 diff --git a/src/nvcore/StrLib.h b/src/nvcore/StrLib.h
450 index 8012271..4164cf5 100644
451 --- a/src/nvcore/StrLib.h
452 +++ b/src/nvcore/StrLib.h
453 @@ -294,7 +294,7 @@ namespace nv
454 const uint16 count = getRefCount();
455 setRefCount(count - 1);
456 if (count - 1 == 0) {
457 - mem::free(data - 2);
462 @@ -323,7 +323,7 @@ namespace nv
464 void allocString(const char * str, int len)
466 - const char * ptr = static_cast<const char *>(mem::malloc(2 + len + 1));
467 + const char * ptr = static_cast<const char *>(::malloc(2 + len + 1));
471 diff --git a/src/nvimage/FloatImage.cpp b/src/nvimage/FloatImage.cpp
472 index 90818ca..6e82ade 100644
473 --- a/src/nvimage/FloatImage.cpp
474 +++ b/src/nvimage/FloatImage.cpp
475 @@ -148,13 +148,13 @@ void FloatImage::allocate(uint c, uint w, uint h)
479 - m_mem = reinterpret_cast<float *>(nv::mem::malloc(m_count * sizeof(float)));
480 + m_mem = reinterpret_cast<float *>(::malloc(m_count * sizeof(float)));
483 /// Free the image, but don't clear the members.
484 void FloatImage::free()
486 - nv::mem::free( reinterpret_cast<void *>(m_mem) );
487 + ::free( reinterpret_cast<void *>(m_mem) );
491 diff --git a/src/nvimage/Image.cpp b/src/nvimage/Image.cpp
492 index 2307d5c..53c0b5f 100644
493 --- a/src/nvimage/Image.cpp
494 +++ b/src/nvimage/Image.cpp
495 @@ -78,7 +78,7 @@ void Image::unwrap()
499 - nv::mem::free(m_data);
504 diff --git a/src/nvimage/ImageIO.cpp b/src/nvimage/ImageIO.cpp
505 index 0b24600..70949ff 100644
506 --- a/src/nvimage/ImageIO.cpp
507 +++ b/src/nvimage/ImageIO.cpp
508 @@ -954,7 +954,7 @@ FloatImage * nv::ImageIO::loadFloatTIFF(const char * fileName, Stream & s)
509 fimage->allocate(spp, width, height);
511 int linesize = TIFFScanlineSize(tif);
512 - tdata_t buf = (::uint8 *)nv::mem::malloc(linesize);
513 + tdata_t buf = (::uint8 *)::malloc(linesize);
515 for (uint y = 0; y < height; y++)
517 @@ -991,7 +991,7 @@ FloatImage * nv::ImageIO::loadFloatTIFF(const char * fileName, Stream & s)
521 - nv::mem::free(buf);
526 diff --git a/src/nvtt/CompressRGB.cpp b/src/nvtt/CompressRGB.cpp
527 index 35239c4..7a6564d 100644
528 --- a/src/nvtt/CompressRGB.cpp
529 +++ b/src/nvtt/CompressRGB.cpp
531 -// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
\r
533 -// Permission is hereby granted, free of charge, to any person
\r
534 -// obtaining a copy of this software and associated documentation
\r
535 -// files (the "Software"), to deal in the Software without
\r
536 -// restriction, including without limitation the rights to use,
\r
537 -// copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
538 -// copies of the Software, and to permit persons to whom the
\r
539 -// Software is furnished to do so, subject to the following
\r
542 -// The above copyright notice and this permission notice shall be
\r
543 -// included in all copies or substantial portions of the Software.
\r
545 -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
546 -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
\r
547 -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
548 -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
\r
549 -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
\r
550 -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
\r
551 -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
\r
552 -// OTHER DEALINGS IN THE SOFTWARE.
\r
554 -#include <nvcore/Debug.h>
\r
556 -#include <nvimage/Image.h>
\r
557 -#include <nvimage/PixelFormat.h>
\r
558 -#include <nvmath/Color.h>
\r
560 -#include "CompressRGB.h"
\r
561 -#include "CompressionOptions.h"
\r
562 -#include "OutputOptions.h"
\r
564 -using namespace nv;
\r
565 -using namespace nvtt;
\r
570 - inline uint computePitch(uint w, uint bitsize)
\r
572 - uint p = w * ((bitsize + 7) / 8);
\r
574 - // Align to 32 bits.
\r
575 - return ((p + 3) / 4) * 4;
\r
578 - inline void convert_to_a8r8g8b8(const void * src, void * dst, uint w)
\r
580 - memcpy(dst, src, 4 * w);
\r
583 - inline void convert_to_x8r8g8b8(const void * src, void * dst, uint w)
\r
585 - memcpy(dst, src, 4 * w);
\r
591 -// Pixel format converter.
\r
592 -void nv::compressRGB(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions)
\r
594 - nvCheck(image != NULL);
\r
596 - const uint w = image->width();
\r
597 - const uint h = image->height();
\r
599 - const uint bitCount = compressionOptions.bitcount;
\r
600 - nvCheck(bitCount == 8 || bitCount == 16 || bitCount == 24 || bitCount == 32);
\r
602 - const uint byteCount = bitCount / 8;
\r
604 - const uint rmask = compressionOptions.rmask;
\r
605 - uint rshift, rsize;
\r
606 - PixelFormat::maskShiftAndSize(rmask, &rshift, &rsize);
\r
608 - const uint gmask = compressionOptions.gmask;
\r
609 - uint gshift, gsize;
\r
610 - PixelFormat::maskShiftAndSize(gmask, &gshift, &gsize);
\r
612 - const uint bmask = compressionOptions.bmask;
\r
613 - uint bshift, bsize;
\r
614 - PixelFormat::maskShiftAndSize(bmask, &bshift, &bsize);
\r
616 - const uint amask = compressionOptions.amask;
\r
617 - uint ashift, asize;
\r
618 - PixelFormat::maskShiftAndSize(amask, &ashift, &asize);
\r
620 - // Determine pitch.
\r
621 - uint pitch = computePitch(w, compressionOptions.bitcount);
\r
623 - uint8 * dst = (uint8 *)mem::malloc(pitch + 4);
\r
625 - for (uint y = 0; y < h; y++)
\r
627 - const Color32 * src = image->scanline(y);
\r
629 - if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0xFF000000)
\r
631 - convert_to_a8r8g8b8(src, dst, w);
\r
633 - else if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0)
\r
635 - convert_to_x8r8g8b8(src, dst, w);
\r
639 - // Generic pixel format conversion.
\r
640 - for (uint x = 0; x < w; x++)
\r
643 - c |= PixelFormat::convert(src[x].r, 8, rsize) << rshift;
\r
644 - c |= PixelFormat::convert(src[x].g, 8, gsize) << gshift;
\r
645 - c |= PixelFormat::convert(src[x].b, 8, bsize) << bshift;
\r
646 - c |= PixelFormat::convert(src[x].a, 8, asize) << ashift;
\r
648 - // Output one byte at a time.
\r
649 - for (uint i = 0; i < byteCount; i++)
\r
651 - *(dst + x * byteCount + i) = (c >> (i * 8)) & 0xFF;
\r
656 - for (uint x = w * byteCount; x < pitch; x++)
\r
662 - if (outputOptions.outputHandler != NULL)
\r
664 - outputOptions.outputHandler->writeData(dst, pitch);
\r
671 +// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
673 +// Permission is hereby granted, free of charge, to any person
674 +// obtaining a copy of this software and associated documentation
675 +// files (the "Software"), to deal in the Software without
676 +// restriction, including without limitation the rights to use,
677 +// copy, modify, merge, publish, distribute, sublicense, and/or sell
678 +// copies of the Software, and to permit persons to whom the
679 +// Software is furnished to do so, subject to the following
682 +// The above copyright notice and this permission notice shall be
683 +// included in all copies or substantial portions of the Software.
685 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
686 +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
687 +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
688 +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
689 +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
690 +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
691 +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
692 +// OTHER DEALINGS IN THE SOFTWARE.
694 +#include <nvcore/Debug.h>
696 +#include <nvimage/Image.h>
697 +#include <nvimage/PixelFormat.h>
698 +#include <nvmath/Color.h>
700 +#include "CompressRGB.h"
701 +#include "CompressionOptions.h"
702 +#include "OutputOptions.h"
705 +using namespace nvtt;
710 + inline uint computePitch(uint w, uint bitsize)
712 + uint p = w * ((bitsize + 7) / 8);
714 + // Align to 32 bits.
715 + return ((p + 3) / 4) * 4;
718 + inline void convert_to_a8r8g8b8(const void * src, void * dst, uint w)
720 + memcpy(dst, src, 4 * w);
723 + inline void convert_to_x8r8g8b8(const void * src, void * dst, uint w)
725 + memcpy(dst, src, 4 * w);
731 +// Pixel format converter.
732 +void nv::compressRGB(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions)
734 + nvCheck(image != NULL);
736 + const uint w = image->width();
737 + const uint h = image->height();
739 + const uint bitCount = compressionOptions.bitcount;
740 + nvCheck(bitCount == 8 || bitCount == 16 || bitCount == 24 || bitCount == 32);
742 + const uint byteCount = bitCount / 8;
744 + const uint rmask = compressionOptions.rmask;
745 + uint rshift, rsize;
746 + PixelFormat::maskShiftAndSize(rmask, &rshift, &rsize);
748 + const uint gmask = compressionOptions.gmask;
749 + uint gshift, gsize;
750 + PixelFormat::maskShiftAndSize(gmask, &gshift, &gsize);
752 + const uint bmask = compressionOptions.bmask;
753 + uint bshift, bsize;
754 + PixelFormat::maskShiftAndSize(bmask, &bshift, &bsize);
756 + const uint amask = compressionOptions.amask;
757 + uint ashift, asize;
758 + PixelFormat::maskShiftAndSize(amask, &ashift, &asize);
760 + // Determine pitch.
761 + uint pitch = computePitch(w, compressionOptions.bitcount);
763 + uint8 * dst = (uint8 *)::malloc(pitch + 4);
765 + for (uint y = 0; y < h; y++)
767 + const Color32 * src = image->scanline(y);
769 + if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0xFF000000)
771 + convert_to_a8r8g8b8(src, dst, w);
773 + else if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0)
775 + convert_to_x8r8g8b8(src, dst, w);
779 + // Generic pixel format conversion.
780 + for (uint x = 0; x < w; x++)
783 + c |= PixelFormat::convert(src[x].r, 8, rsize) << rshift;
784 + c |= PixelFormat::convert(src[x].g, 8, gsize) << gshift;
785 + c |= PixelFormat::convert(src[x].b, 8, bsize) << bshift;
786 + c |= PixelFormat::convert(src[x].a, 8, asize) << ashift;
788 + // Output one byte at a time.
789 + for (uint i = 0; i < byteCount; i++)
791 + *(dst + x * byteCount + i) = (c >> (i * 8)) & 0xFF;
796 + for (uint x = w * byteCount; x < pitch; x++)
802 + if (outputOptions.outputHandler != NULL)
804 + outputOptions.outputHandler->writeData(dst, pitch);
811 diff --git a/src/nvtt/cuda/CudaCompressDXT.cpp b/src/nvtt/cuda/CudaCompressDXT.cpp
812 index c59bedd..6b45ceb 100644
813 --- a/src/nvtt/cuda/CudaCompressDXT.cpp
814 +++ b/src/nvtt/cuda/CudaCompressDXT.cpp
815 @@ -137,7 +137,7 @@ void CudaCompressor::compressDXT1(const CompressionOptions::Private & compressio
816 const uint h = (m_image->height() + 3) / 4;
818 uint imageSize = w * h * 16 * sizeof(Color32);
819 - uint * blockLinearImage = (uint *) malloc(imageSize);
820 + uint * blockLinearImage = (uint *) ::malloc(imageSize);
821 convertToBlockLinear(m_image, blockLinearImage); // @@ Do this in parallel with the GPU, or in the GPU!
823 const uint blockNum = w * h;
824 @@ -207,14 +207,14 @@ void CudaCompressor::compressDXT3(const CompressionOptions::Private & compressio
825 const uint h = (m_image->height() + 3) / 4;
827 uint imageSize = w * h * 16 * sizeof(Color32);
828 - uint * blockLinearImage = (uint *) malloc(imageSize);
829 + uint * blockLinearImage = (uint *) ::malloc(imageSize);
830 convertToBlockLinear(m_image, blockLinearImage);
832 const uint blockNum = w * h;
833 const uint compressedSize = blockNum * 8;
835 AlphaBlockDXT3 * alphaBlocks = NULL;
836 - alphaBlocks = (AlphaBlockDXT3 *)malloc(min(compressedSize, MAX_BLOCKS * 8U));
837 + alphaBlocks = (AlphaBlockDXT3 *)::malloc(min(compressedSize, MAX_BLOCKS * 8U));
839 setupCompressKernel(compressionOptions.colorWeight.ptr());
841 @@ -298,14 +298,14 @@ void CudaCompressor::compressDXT5(const CompressionOptions::Private & compressio
842 const uint h = (m_image->height() + 3) / 4;
844 uint imageSize = w * h * 16 * sizeof(Color32);
845 - uint * blockLinearImage = (uint *) malloc(imageSize);
846 + uint * blockLinearImage = (uint *) ::malloc(imageSize);
847 convertToBlockLinear(m_image, blockLinearImage);
849 const uint blockNum = w * h;
850 const uint compressedSize = blockNum * 8;
852 AlphaBlockDXT5 * alphaBlocks = NULL;
853 - alphaBlocks = (AlphaBlockDXT5 *)malloc(min(compressedSize, MAX_BLOCKS * 8U));
854 + alphaBlocks = (AlphaBlockDXT5 *)::malloc(min(compressedSize, MAX_BLOCKS * 8U));
856 setupCompressKernel(compressionOptions.colorWeight.ptr());