]> Pileus Git - ~andy/sunrise/blob - dev-util/nvidia-texture-tools/files/valgrind.patch
dev-python/mimeparse: Moved to gentoo-x86 by tampakrap.
[~andy/sunrise] / dev-util / nvidia-texture-tools / files / valgrind.patch
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
5
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.
11  
12  
13  // nvcore
14 -#include <nvcore/nvcore.h>
15 -#include <nvcore/Memory.h>
16 -#include <nvcore/Debug.h>
17 +#include "nvcore.h"
18 +#include "Memory.h"
19 +#include "Debug.h"
20  
21  #include <string.h>    // memmove
22  #include <new>         // for placement new
23 @@ -589,15 +589,15 @@ namespace nv
24                         // free the buffer.
25                         if( m_buffer_size == 0 ) {
26                                 if( m_buffer ) {
27 -                                       mem::free( m_buffer );
28 +                                       free( m_buffer );
29                                         m_buffer = NULL;
30                                 }
31                         }
32                         
33                         // realloc the buffer
34                         else {
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);
39                         }
40                 }
41                 
42 @@ -778,7 +778,7 @@ namespace nv
43                                                 e->clear();
44                                         }
45                                 }
46 -                               mem::free(table);
47 +                               free(table);
48                                 table = NULL;
49                                 entry_count = 0;
50                                 size_mask = -1;
51 @@ -1001,7 +1001,7 @@ namespace nv
52                         new_size = nextPowerOfTwo(new_size);
53                         
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);
58                         
59                         new_hash.entry_count = 0;
60 @@ -1026,7 +1026,7 @@ namespace nv
61                                 }
62                                 
63                                 // Delete our old data buffer.
64 -                               mem::free(table);
65 +                               free(table);
66                         }
67                         
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
73 @@ -1,36 +1,118 @@
74 +// This code is in the public domain -- Ignacio CastaƱo <castano@gmail.com>
75  
76  #include "Memory.h"
77  #include "Debug.h"
78  
79 -//#if HAVE_MALLOC_H
80 -//#include <malloc.h>
81 -//#endif
82 -
83  #include <stdlib.h>
84  
85 +#define USE_EFENCE 0
86 +
87 +#if USE_EFENCE
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);
91 +#endif
92  
93  using namespace nv;
94  
95 -void * nv::mem::malloc(size_t size)
96 +#if NV_OVERRIDE_ALLOC
97 +
98 +void * malloc(size_t size)
99 +{
100 +#if USE_EFENCE
101 +    return EF_malloc(size);
102 +#else
103 +    return ::malloc(size);
104 +#endif
105 +}
106 +
107 +void * debug_malloc(size_t size, const char * file, int line)
108 +{
109 +    NV_UNUSED(file);
110 +    NV_UNUSED(line);
111 +#if USE_EFENCE
112 +    return EF_malloc(size);
113 +#else
114 +    return ::malloc(size);
115 +#endif
116 +}
117 +
118 +void free(void * ptr)
119  {
120 -       return ::malloc(size);
121 +#if USE_EFENCE
122 +    return EF_free(const_cast<void *>(ptr));
123 +#else
124 +    ::free(const_cast<void *>(ptr));
125 +#endif
126  }
127  
128 -void * nv::mem::malloc(size_t size, const char * file, int line)
129 +void * realloc(void * ptr, size_t size)
130  {
131 -       NV_UNUSED(file);
132 -       NV_UNUSED(line);
133 -       return ::malloc(size);
134 +    nvDebugCheck(ptr != NULL || size != 0); // undefined realloc behavior.
135 +#if USE_EFENCE
136 +    return EF_realloc(ptr, size);
137 +#else
138 +    return ::realloc(ptr, size);
139 +#endif
140  }
141  
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()
145  {
146 -       ::free(const_cast<void *>(ptr));
147 +    return malloc(size);
148  }
149  
150 -void * nv::mem::realloc(void * ptr, size_t size)
151 +void operator delete (void *p) throw()
152  {
153 -       nvDebugCheck(ptr != NULL || size != 0); // undefined realloc behavior.
154 -       return ::realloc(ptr, size);
155 +    free(p);
156  }
157  
158 +void * operator new [] (size_t size) throw()
159 +{
160 +    return malloc(size);
161 +}
162 +
163 +void operator delete [] (void * p) throw()
164 +{
165 +    free(p);
166 +}
167 +*/
168 +
169 +#if 0 // Code from Apple:
170 +void* operator new(std::size_t sz) throw (std::bad_alloc)
171 +{
172 +        void *result = std::malloc (sz == 0 ? 1 : sz);
173 +        if (result == NULL)
174 +                throw std::bad_alloc();
175 +        gNewCounter++;
176 +        return result;
177 +}
178 +void operator delete(void* p) throw()
179 +{
180 +        if (p == NULL)
181 +                return;
182 +        std::free (p);
183 +        gDeleteCounter++;
184 +}
185 +
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()
190 +{
191 +        try {
192 +                void * result = ::operator new (sz);  // calls our overridden operator new
193 +                return result;
194 +        } catch (std::bad_alloc &) {
195 +          return NULL;
196 +        }
197 +}
198 +void operator delete(void* p, const std::nothrow_t&) throw()
199 +{
200 +        ::operator delete (p);
201 +}
202 +
203 +#endif // 0
204 +
205 +
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
211 @@ -1,186 +1,52 @@
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>
214  
215 +#pragma once
216  #ifndef NV_CORE_MEMORY_H
217  #define NV_CORE_MEMORY_H
218  
219 -#include <nvcore/nvcore.h>
220 +#include "nvcore.h"
221  
222  #include <stdlib.h> // malloc(), realloc() and free()
223 -#include <stddef.h>    // size_t
224 +#include <stddef.h> // size_t
225  
226  #include <new> // new and delete
227  
228 -// Custom memory allocator
229 -namespace nv
230 -{
231 -       namespace mem 
232 -       {
233 -               NVCORE_API void * malloc(size_t size);
234 -               NVCORE_API void * malloc(size_t size, const char * file, int line);
235 -               
236 -               NVCORE_API void free(const void * ptr);
237 -               NVCORE_API void * realloc(void * ptr, size_t size);
238 -               
239 -       } // mem namespace
240 -       
241 -} // nv namespace
242 -
243 -
244 -// Override new/delete
245 +#define NV_OVERRIDE_ALLOC 0
246  
247 -inline void * operator new (size_t size) throw()
248 -{
249 -       return nv::mem::malloc(size); 
250 -}
251 -
252 -inline void operator delete (void *p) throw()
253 -{
254 -       nv::mem::free(p); 
255 -}
256 +#if NV_OVERRIDE_ALLOC
257  
258 -inline void * operator new [] (size_t size) throw()
259 -{
260 -       return nv::mem::malloc(size);
261 -}
262 -
263 -inline void operator delete [] (void * p) throw()
264 -{
265 -       nv::mem::free(p); 
266 +// Custom memory allocator
267 +extern "C" {
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);
272  }
273  
274  /*
275  #ifdef _DEBUG
276  #define new new(__FILE__, __LINE__)
277 -#define malloc(i) malloc(i, __FILE__, __LINE__)
278 +#define malloc(i) debug_malloc(i, __FILE__, __LINE__)
279  #endif
280  */
281  
282 -#if 0
283 -/*
284 -    File:      main.cpp
285 -    
286 -    Version:   1.0
287 -
288 -       Abstract: Overrides the C++ 'operator new' and 'operator delete'.
289 -
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.
295 -
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.
310 -
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.
316 -
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.
324 -
325 -       Copyright Ā© 2006 Apple Computer, Inc., All Rights Reserved
326 -*/
327 -
328 -/* This sample shows how to override the C++ global 'new' and 'delete' operators.  */
329 -#include <new>
330 -#include <iostream>
331 -#include <cstdlib>
332 -#include <stdexcept>
333 -#include <locale>
334 -
335 -/* Some variables and code to make the example do something.  */
336 -namespace {
337 -  unsigned long long gNewCounter; // number of times 'new' was called
338 -  unsigned long long gDeleteCounter;  // number of times 'delete' was called
339 -  
340 -  void printCounters()  // print the counters above
341 -  {
342 -       std::cout << "new was called " << gNewCounter << " times and delete was called " << gDeleteCounter << " times\n";
343 -  }
344 -}
345 -
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.
348 -
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.
352 +#endif
353  
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
356 -   requirements.
357 +namespace nv {
358  
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)
361 -{
362 -       void *result = std::malloc (sz == 0 ? 1 : sz);
363 -       if (result == NULL)
364 -               throw std::bad_alloc();
365 -       gNewCounter++;
366 -       return result;
367 -}
368 -void operator delete(void* p) throw()
369 -{
370 -       if (p == NULL)
371 -               return;
372 -       std::free (p);
373 -       gDeleteCounter++;
374 -}
375 +    // C++ helpers.
376 +    template <typename T> T * malloc(size_t count) {
377 +        return (T *)::malloc(sizeof(T) * count);
378 +    }
379  
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()
384 -{
385 -       try {
386 -               void * result = ::operator new (sz);  // calls our overridden operator new
387 -               return result;
388 -       } catch (std::bad_alloc &) {
389 -         return NULL;
390 -       }
391 -}
392 -void operator delete(void* p, const std::nothrow_t&) throw()
393 -{
394 -       ::operator delete (p);
395 -}
396 +    template <typename T> T * realloc(T * ptr, size_t count) {
397 +        return (T *)::realloc(ptr, sizeof(T) * count);
398 +    }
399  
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);
408 +    }
409  
410 -/* This is a simple test program that causes the runtime library to call new and delete.  */
411 -int main() 
412 -{
413 -       atexit (printCounters);
414 -       try {
415 -         std::locale example("does_not_exist");
416 -       } catch (std::runtime_error &x) {
417 -       }
418 -       return 0;
419 -}
420 -#endif // 0
421 +} // nv namespace
422  
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
429  {
430         static char * strAlloc(uint size)
431         {
432 -               return static_cast<char *>(mem::malloc(size));
433 +               return static_cast<char *>(::malloc(size));
434         }
435         
436         static char * strReAlloc(char * str, uint size)
437         {
438 -               return static_cast<char *>(mem::realloc(str, size));
439 +               return static_cast<char *>(::realloc(str, size));
440         }
441         
442         static void strFree(const char * str)
443         {
444 -               return mem::free(const_cast<char *>(str));
445 +               return ::free(const_cast<char *>(str));
446         }
447         
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);
458 +                                       free(data - 2);
459                                         data = NULL;
460                                 }
461                         }
462 @@ -323,7 +323,7 @@ namespace nv
463  
464                 void allocString(const char * str, int len)
465                 {
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));
468         
469                         setData( ptr );                         
470                         setRefCount( 0 );
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)
476         m_height = h;
477         m_componentNum = c;
478         m_count = w * h * c;
479 -       m_mem = reinterpret_cast<float *>(nv::mem::malloc(m_count * sizeof(float)));
480 +       m_mem = reinterpret_cast<float *>(::malloc(m_count * sizeof(float)));
481  }
482  
483  /// Free the image, but don't clear the members.
484  void FloatImage::free()
485  {
486 -       nv::mem::free( reinterpret_cast<void *>(m_mem) );
487 +       ::free( reinterpret_cast<void *>(m_mem) );
488         m_mem = NULL;
489  }
490  
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()
496  
497  void Image::free()
498  {
499 -       nv::mem::free(m_data);
500 +       ::free(m_data);
501         m_data = NULL;
502  }
503  
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);
510         
511         int linesize = TIFFScanlineSize(tif);
512 -       tdata_t buf = (::uint8 *)nv::mem::malloc(linesize);
513 +       tdata_t buf = (::uint8 *)::malloc(linesize);
514         
515         for (uint y = 0; y < height; y++) 
516         {
517 @@ -991,7 +991,7 @@ FloatImage * nv::ImageIO::loadFloatTIFF(const char * fileName, Stream & s)
518                 }
519         }
520  
521 -       nv::mem::free(buf);
522 +       ::free(buf);
523         
524         TIFFClose(tif);
525         
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
530 @@ -1,140 +1,140 @@
531 -// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>\r
532 -// \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
540 -// conditions:\r
541 -// \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
544 -// \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
553 -\r
554 -#include <nvcore/Debug.h>\r
555 -\r
556 -#include <nvimage/Image.h>\r
557 -#include <nvimage/PixelFormat.h>\r
558 -#include <nvmath/Color.h>\r
559 -\r
560 -#include "CompressRGB.h"\r
561 -#include "CompressionOptions.h"\r
562 -#include "OutputOptions.h"\r
563 -\r
564 -using namespace nv;\r
565 -using namespace nvtt;\r
566 -\r
567 -namespace \r
568 -{\r
569 -\r
570 -       inline uint computePitch(uint w, uint bitsize)\r
571 -       {\r
572 -               uint p = w * ((bitsize + 7) / 8);\r
573 -\r
574 -               // Align to 32 bits.\r
575 -               return ((p + 3) / 4) * 4;\r
576 -       }\r
577 -\r
578 -       inline void convert_to_a8r8g8b8(const void * src, void * dst, uint w)\r
579 -       {\r
580 -               memcpy(dst, src, 4 * w);\r
581 -       }\r
582 -\r
583 -       inline void convert_to_x8r8g8b8(const void * src, void * dst, uint w)\r
584 -       {\r
585 -               memcpy(dst, src, 4 * w);\r
586 -       }\r
587 -\r
588 -} // namespace\r
589 -\r
590 -\r
591 -// Pixel format converter.\r
592 -void nv::compressRGB(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions)\r
593 -{\r
594 -       nvCheck(image != NULL);\r
595 -\r
596 -       const uint w = image->width();\r
597 -       const uint h = image->height();\r
598 -\r
599 -       const uint bitCount = compressionOptions.bitcount;\r
600 -       nvCheck(bitCount == 8 || bitCount == 16 || bitCount == 24 || bitCount == 32);\r
601 -\r
602 -       const uint byteCount = bitCount / 8;\r
603 -\r
604 -       const uint rmask = compressionOptions.rmask;\r
605 -       uint rshift, rsize;\r
606 -       PixelFormat::maskShiftAndSize(rmask, &rshift, &rsize);\r
607 -       \r
608 -       const uint gmask = compressionOptions.gmask;\r
609 -       uint gshift, gsize;\r
610 -       PixelFormat::maskShiftAndSize(gmask, &gshift, &gsize);\r
611 -       \r
612 -       const uint bmask = compressionOptions.bmask;\r
613 -       uint bshift, bsize;\r
614 -       PixelFormat::maskShiftAndSize(bmask, &bshift, &bsize);\r
615 -       \r
616 -       const uint amask = compressionOptions.amask;\r
617 -       uint ashift, asize;\r
618 -       PixelFormat::maskShiftAndSize(amask, &ashift, &asize);\r
619 -\r
620 -       // Determine pitch.\r
621 -       uint pitch = computePitch(w, compressionOptions.bitcount);\r
622 -\r
623 -       uint8 * dst = (uint8 *)mem::malloc(pitch + 4);\r
624 -\r
625 -       for (uint y = 0; y < h; y++)\r
626 -       {\r
627 -               const Color32 * src = image->scanline(y);\r
628 -\r
629 -               if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0xFF000000)\r
630 -               {\r
631 -                       convert_to_a8r8g8b8(src, dst, w);\r
632 -               }\r
633 -               else if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0)\r
634 -               {\r
635 -                       convert_to_x8r8g8b8(src, dst, w);\r
636 -               }\r
637 -               else\r
638 -               {\r
639 -                       // Generic pixel format conversion.\r
640 -                       for (uint x = 0; x < w; x++)\r
641 -                       {\r
642 -                               uint c = 0;\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
647 -                               \r
648 -                               // Output one byte at a time.\r
649 -                               for (uint i = 0; i < byteCount; i++)\r
650 -                               {\r
651 -                                       *(dst + x * byteCount + i) = (c >> (i * 8)) & 0xFF;\r
652 -                               }\r
653 -                       }\r
654 -                       \r
655 -                       // Zero padding.\r
656 -                       for (uint x = w * byteCount; x < pitch; x++)\r
657 -                       {\r
658 -                               *(dst + x) = 0;\r
659 -                       }\r
660 -               }\r
661 -\r
662 -               if (outputOptions.outputHandler != NULL)\r
663 -               {\r
664 -                       outputOptions.outputHandler->writeData(dst, pitch);\r
665 -               }\r
666 -       }\r
667 -\r
668 -       mem::free(dst);\r
669 -}\r
670 -\r
671 +// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
672 +// 
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
680 +// conditions:
681 +// 
682 +// The above copyright notice and this permission notice shall be
683 +// included in all copies or substantial portions of the Software.
684 +// 
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.
693 +
694 +#include <nvcore/Debug.h>
695 +
696 +#include <nvimage/Image.h>
697 +#include <nvimage/PixelFormat.h>
698 +#include <nvmath/Color.h>
699 +
700 +#include "CompressRGB.h"
701 +#include "CompressionOptions.h"
702 +#include "OutputOptions.h"
703 +
704 +using namespace nv;
705 +using namespace nvtt;
706 +
707 +namespace 
708 +{
709 +
710 +       inline uint computePitch(uint w, uint bitsize)
711 +       {
712 +               uint p = w * ((bitsize + 7) / 8);
713 +
714 +               // Align to 32 bits.
715 +               return ((p + 3) / 4) * 4;
716 +       }
717 +
718 +       inline void convert_to_a8r8g8b8(const void * src, void * dst, uint w)
719 +       {
720 +               memcpy(dst, src, 4 * w);
721 +       }
722 +
723 +       inline void convert_to_x8r8g8b8(const void * src, void * dst, uint w)
724 +       {
725 +               memcpy(dst, src, 4 * w);
726 +       }
727 +
728 +} // namespace
729 +
730 +
731 +// Pixel format converter.
732 +void nv::compressRGB(const Image * image, const OutputOptions::Private & outputOptions, const CompressionOptions::Private & compressionOptions)
733 +{
734 +       nvCheck(image != NULL);
735 +
736 +       const uint w = image->width();
737 +       const uint h = image->height();
738 +
739 +       const uint bitCount = compressionOptions.bitcount;
740 +       nvCheck(bitCount == 8 || bitCount == 16 || bitCount == 24 || bitCount == 32);
741 +
742 +       const uint byteCount = bitCount / 8;
743 +
744 +       const uint rmask = compressionOptions.rmask;
745 +       uint rshift, rsize;
746 +       PixelFormat::maskShiftAndSize(rmask, &rshift, &rsize);
747 +       
748 +       const uint gmask = compressionOptions.gmask;
749 +       uint gshift, gsize;
750 +       PixelFormat::maskShiftAndSize(gmask, &gshift, &gsize);
751 +       
752 +       const uint bmask = compressionOptions.bmask;
753 +       uint bshift, bsize;
754 +       PixelFormat::maskShiftAndSize(bmask, &bshift, &bsize);
755 +       
756 +       const uint amask = compressionOptions.amask;
757 +       uint ashift, asize;
758 +       PixelFormat::maskShiftAndSize(amask, &ashift, &asize);
759 +
760 +       // Determine pitch.
761 +       uint pitch = computePitch(w, compressionOptions.bitcount);
762 +
763 +       uint8 * dst = (uint8 *)::malloc(pitch + 4);
764 +
765 +       for (uint y = 0; y < h; y++)
766 +       {
767 +               const Color32 * src = image->scanline(y);
768 +
769 +               if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0xFF000000)
770 +               {
771 +                       convert_to_a8r8g8b8(src, dst, w);
772 +               }
773 +               else if (bitCount == 32 && rmask == 0xFF0000 && gmask == 0xFF00 && bmask == 0xFF && amask == 0)
774 +               {
775 +                       convert_to_x8r8g8b8(src, dst, w);
776 +               }
777 +               else
778 +               {
779 +                       // Generic pixel format conversion.
780 +                       for (uint x = 0; x < w; x++)
781 +                       {
782 +                               uint c = 0;
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;
787 +                               
788 +                               // Output one byte at a time.
789 +                               for (uint i = 0; i < byteCount; i++)
790 +                               {
791 +                                       *(dst + x * byteCount + i) = (c >> (i * 8)) & 0xFF;
792 +                               }
793 +                       }
794 +                       
795 +                       // Zero padding.
796 +                       for (uint x = w * byteCount; x < pitch; x++)
797 +                       {
798 +                               *(dst + x) = 0;
799 +                       }
800 +               }
801 +
802 +               if (outputOptions.outputHandler != NULL)
803 +               {
804 +                       outputOptions.outputHandler->writeData(dst, pitch);
805 +               }
806 +       }
807 +
808 +       ::free(dst);
809 +}
810 +
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;
817  
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!
822  
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;
826  
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);
831  
832         const uint blockNum = w * h;
833         const uint compressedSize = blockNum * 8;
834  
835         AlphaBlockDXT3 * alphaBlocks = NULL;
836 -       alphaBlocks = (AlphaBlockDXT3 *)malloc(min(compressedSize, MAX_BLOCKS * 8U));
837 +       alphaBlocks = (AlphaBlockDXT3 *)::malloc(min(compressedSize, MAX_BLOCKS * 8U));
838  
839         setupCompressKernel(compressionOptions.colorWeight.ptr());
840         
841 @@ -298,14 +298,14 @@ void CudaCompressor::compressDXT5(const CompressionOptions::Private & compressio
842         const uint h = (m_image->height() + 3) / 4;
843  
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);
848  
849         const uint blockNum = w * h;
850         const uint compressedSize = blockNum * 8;
851  
852         AlphaBlockDXT5 * alphaBlocks = NULL;
853 -       alphaBlocks = (AlphaBlockDXT5 *)malloc(min(compressedSize, MAX_BLOCKS * 8U));
854 +       alphaBlocks = (AlphaBlockDXT5 *)::malloc(min(compressedSize, MAX_BLOCKS * 8U));
855  
856         setupCompressKernel(compressionOptions.colorWeight.ptr());
857