Skip to content

Commit

Permalink
Move calling convention for native imports into Constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jzebedee committed Sep 19, 2023
1 parent 7f3c9fc commit 198e0e3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/LibDeflate/Imports/Checksums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class Checksums
/// required initial value for 'adler' is 1. This value is also returned when
/// 'buffer' is specified as NULL.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern uint libdeflate_adler32(uint adler, in byte buffer, size_t len);

///<summary>
Expand All @@ -21,6 +21,6 @@ internal static class Checksums
/// initial value for 'crc' is 0. This value is also returned when 'buffer' is
/// specified as NULL.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern uint libdeflate_crc32(uint crc, in byte buffer, size_t len);
}
18 changes: 9 additions & 9 deletions src/LibDeflate/Imports/Compression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ internal static class Compression
/// A single compressor is not safe to use by multiple threads concurrently.
/// However, different threads may use different compressors concurrently.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_compressor libdeflate_alloc_compressor(int compression_level);

/// <summary>
/// Like <see cref="libdeflate_alloc_compressor"/> but allows specifying advanced options per-compressor.
/// </summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_compressor libdeflate_alloc_compressor_ex(int compression_level, in libdeflate_options options);

///<summary>
Expand All @@ -41,7 +41,7 @@ internal static class Compression
/// bytes. The return value is the compressed size in bytes, or 0 if the data
/// could not be compressed to 'out_nbytes_avail' bytes or fewer.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_deflate_compress(libdeflate_compressor compressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail);

///<summary>
Expand Down Expand Up @@ -69,44 +69,44 @@ internal static class Compression
/// libdeflate_deflate_compress() returns 0, indicating that the compressed data
/// did not fit into the provided output buffer.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_deflate_compress_bound(libdeflate_compressor compressor, size_t in_nbytes);

///<summary>
/// Like libdeflate_deflate_compress(), but stores the data in the zlib wrapper
/// format.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_zlib_compress(libdeflate_compressor compressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail);

///<summary>
/// Like libdeflate_deflate_compress_bound(), but assumes the data will be
/// compressed with libdeflate_zlib_compress() rather than with
/// libdeflate_deflate_compress().
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_zlib_compress_bound(libdeflate_compressor compressor, size_t in_nbytes);

///<summary>
/// Like libdeflate_deflate_compress(), but stores the data in the gzip wrapper
/// format.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_gzip_compress(libdeflate_compressor compressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail);

///<summary>
/// Like libdeflate_deflate_compress_bound(), but assumes the data will be
/// compressed with libdeflate_gzip_compress() rather than with
/// libdeflate_deflate_compress().
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_gzip_compress_bound(libdeflate_compressor compressor, size_t in_nbytes);

///<summary>
/// libdeflate_free_compressor() frees a compressor that was allocated with
/// libdeflate_alloc_compressor(). If a NULL pointer is passed in, no action is
/// taken.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern void libdeflate_free_compressor(libdeflate_compressor compressor);
}
2 changes: 2 additions & 0 deletions src/LibDeflate/Imports/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: InternalsVisibleTo($"{nameof(LibDeflate)}.Tests")]
[assembly: InternalsVisibleTo($"{nameof(LibDeflate)}.DangerousTests")]
Expand All @@ -7,4 +8,5 @@ namespace LibDeflate.Imports;
internal static class Constants
{
public const string DllName = "libdeflate";
public const CallingConvention CallConv = CallingConvention.Cdecl;
}
2 changes: 1 addition & 1 deletion src/LibDeflate/Imports/CustomMemoryAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ internal static class CustomMemoryAllocator
/// There must not be any libdeflate_compressor or libdeflate_decompressor
/// structures in existence when calling this function.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern void libdeflate_set_memory_allocator(malloc_func malloc, free_func free);
}
18 changes: 9 additions & 9 deletions src/LibDeflate/Imports/Decompression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public enum libdeflate_result
/// A single decompressor is not safe to use by multiple threads concurrently.
/// However, different threads may use different decompressors concurrently.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_decompressor libdeflate_alloc_decompressor();

/// <summary>
/// Like <see cref="libdeflate_alloc_decompressor"/> but allows specifying advanced options per-decompressor.
/// </summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_decompressor libdeflate_alloc_decompressor_ex(in libdeflate_options options);

///<summary>
Expand Down Expand Up @@ -89,7 +89,7 @@ public enum libdeflate_result
/// not large enough but no other problems were encountered, or another
/// nonzero result code if decompression failed for another reason.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_deflate_decompress(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -98,7 +98,7 @@ public enum libdeflate_result
/// then the actual compressed size of the DEFLATE stream (aligned to the next
/// byte boundary) is written to *actual_in_nbytes_ret.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_deflate_decompress_ex(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_in_nbytes_ret, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -109,7 +109,7 @@ public enum libdeflate_result
/// than 'in_nbytes'. If you need to know exactly where the zlib stream ended,
/// use libdeflate_zlib_decompress_ex().
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_zlib_decompress(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -120,7 +120,7 @@ public enum libdeflate_result
/// than 'in_nbytes'. If you need to know exactly where the zlib stream ended,
/// use libdeflate_zlib_decompress_ex().
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_zlib_decompress_ex(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_in_nbytes_ret, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -131,7 +131,7 @@ public enum libdeflate_result
/// will be decompressed. Use libdeflate_gzip_decompress_ex() if you need
/// multi-member support.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_gzip_decompress(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -141,14 +141,14 @@ public enum libdeflate_result
/// buffer was decompressed), then the actual number of input bytes consumed is
/// written to *actual_in_nbytes_ret.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_gzip_decompress_ex(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_in_nbytes_ret, out size_t actual_out_nbytes_ret);

///<summary>
/// libdeflate_free_decompressor() frees a decompressor that was allocated with
/// libdeflate_alloc_decompressor(). If a NULL pointer is passed in, no action
/// is taken.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern void libdeflate_free_decompressor(libdeflate_decompressor decompressor);
}

0 comments on commit 198e0e3

Please sign in to comment.