home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Multimedia / AVStoDVD / AVStoDVD_280_Install.exe / AVSMeter / source / avs / alignment.h next >
C/C++ Source or Header  |  2014-03-02  |  4KB  |  125 lines

  1. // Avisynth C Interface Version 0.20
  2. // Copyright 2003 Kevin Atkinson
  3.  
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
  17. // http://www.gnu.org/copyleft/gpl.html .
  18. //
  19. // As a special exception, I give you permission to link to the
  20. // Avisynth C interface with independent modules that communicate with
  21. // the Avisynth C interface solely through the interfaces defined in
  22. // avisynth_c.h, regardless of the license terms of these independent
  23. // modules, and to copy and distribute the resulting combined work
  24. // under terms of your choice, provided that every copy of the
  25. // combined work is accompanied by a complete copy of the source code
  26. // of the Avisynth C interface and Avisynth itself (with the version
  27. // used to produce the combined work), being distributed under the
  28. // terms of the GNU General Public License plus this exception.  An
  29. // independent module is a module which is not derived from or based
  30. // on Avisynth C Interface, such as 3rd-party filters, import and
  31. // export plugins, or graphical user interfaces.
  32.  
  33. #ifndef AVS_ALIGNMENT_H
  34. #define AVS_ALIGNMENT_H
  35.  
  36. // Functions and macros to help work with alignment requirements.
  37.  
  38.  
  39. // Tells if a number is a power of two.
  40. #define IS_POWER2(n) ((n) && !((n) & ((n) - 1)))
  41.  
  42. // Tells if the pointer "ptr" is aligned to "align" bytes.
  43. #define IS_PTR_ALIGNED(ptr, align) (((uintptr_t)ptr & ((uintptr_t)(align-1))) == 0)
  44.  
  45. // Rounds up the number "n" to the next greater multiple of "align"
  46. #define ALIGN_NUMBER(n, align) (((n) + (align)-1) & (~((align)-1)))
  47.  
  48. // Rounds up the pointer address "ptr" to the next greater multiple of "align"
  49. #define ALIGN_POINTER(ptr, align) (((uintptr_t)(ptr) + (align)-1) & (~(uintptr_t)((align)-1)))
  50.  
  51. #ifdef __cplusplus
  52.  
  53. #include <cassert>
  54. #include <cstdlib>
  55.  
  56. template<typename T>
  57. static bool IsPtrAligned(T* ptr, size_t align)
  58. {
  59.   assert(IS_POWER2(align));
  60.   return (bool)IS_PTR_ALIGNED(ptr, align);
  61. }
  62.  
  63. template<typename T>
  64. static T AlignNumber(T n, T align)
  65. {
  66.   assert(IS_POWER2(align));
  67.   return ALIGN_NUMBER(n, align);
  68. }
  69.  
  70. template<typename T>
  71. static T* AlignPointer(T* ptr, size_t align)
  72. {
  73.   assert(IS_POWER2(align));
  74.   return (T*)ALIGN_POINTER(ptr, align);
  75. }
  76.  
  77. extern "C"
  78. {
  79. #else
  80. #include <stdlib.h>
  81. #endif  // __cplusplus
  82.  
  83. // Returns a new buffer that is at least the size "nbytes".
  84. // The buffer will be aligned to "align" bytes.
  85. // Returns NULL on error. On successful allocation,
  86. // the returned buffer must be freed using "avs_free".
  87. inline void* avs_malloc(size_t nbytes, size_t align)
  88. {
  89.   if (!IS_POWER2(align))
  90.     return NULL;
  91.   
  92.   size_t offset = sizeof(void*) + align - 1;
  93.   
  94.   void *orig = malloc(nbytes + offset);
  95.   if (orig == NULL)
  96.    return NULL;
  97.    
  98.   void **aligned = (void**)(((uintptr_t)orig + (uintptr_t)offset) & (~(uintptr_t)(align-1)));
  99.   aligned[-1] = orig;
  100.   return aligned;
  101. }
  102.  
  103. // Buffers allocated using "avs_malloc" must be freed
  104. // using "avs_free" instead of "free".
  105. inline void avs_free(void *ptr)
  106. {
  107.   // Mirroring free()'s semantic requires us to accept NULLs
  108.   if (ptr == NULL)
  109.     return;
  110.     
  111.   free(((void**)ptr)[-1]);
  112. }
  113.  
  114. #ifdef __cplusplus
  115. } // extern "C"
  116.  
  117. // The point of these undef's is to force using the template functions
  118. // if we are in C++ mode. For C, the user can rely only on the macros.
  119. #undef IS_PTR_ALIGNED
  120. #undef ALIGN_NUMBER
  121. #undef ALIGN_POINTER
  122.  
  123. #endif  // __cplusplus
  124.  
  125. #endif  //AVS_ALIGNMENT_H