Fix typo and make `calloc` default
diff --git a/example/main.dart b/example/main.dart
index 4731e42..8dcce72 100644
--- a/example/main.dart
+++ b/example/main.dart
@@ -3,16 +3,16 @@
import 'package:ffi/ffi.dart' hide allocate;
void main() {
- // Allocate and free some native memory with malloc and free.
- final pointer = malloc<Uint8>();
+ // Allocate and free some native memory with calloc and free.
+ final pointer = calloc<Uint8>();
pointer.value = 3;
print(pointer.value);
- malloc.free(pointer);
+ calloc.free(pointer);
// Use the Utf8 helper to encode zero-terminated UTF-8 strings in native memory.
final String myString = 'ππΏπ¬';
final Pointer<Utf8> charPointer = Utf8.toUtf8(myString);
print('First byte is: ${charPointer.cast<Uint8>().value}');
print(Utf8.fromUtf8(charPointer));
- malloc.free(charPointer);
+ calloc.free(charPointer);
}
diff --git a/lib/src/allocation.dart b/lib/src/allocation.dart
index c474119..df1c716 100644
--- a/lib/src/allocation.dart
+++ b/lib/src/allocation.dart
@@ -71,8 +71,8 @@
/// Manages memory on the native heap.
///
-/// Does not intialize newly allocated memory to zero. Use [_CallocAllocator]
-/// for zero-intialized memory on allocation.
+/// Does not initialize newly allocated memory to zero. Use [_CallocAllocator]
+/// for zero-initialized memory on allocation.
///
/// For POSIX-based systems, this uses `malloc` and `free`. On Windows, it uses
/// `HeapAlloc` and `HeapFree` against the default public heap.
@@ -126,8 +126,8 @@
/// Manages memory on the native heap.
///
-/// Does not intialize newly allocated memory to zero. Use [calloc] for
-/// zero-intialized memory allocation.
+/// Does not initialize newly allocated memory to zero. Use [calloc] for
+/// zero-initialized memory allocation.
///
/// For POSIX-based systems, this uses `malloc` and `free`. On Windows, it uses
/// `HeapAlloc` and `HeapFree` against the default public heap.
@@ -192,7 +192,7 @@
/// Manages memory on the native heap.
///
-/// Initializes newly allocated memory to zero. Use [malloc] for unintialized
+/// Initializes newly allocated memory to zero. Use [malloc] for uninitialized
/// memory allocation.
///
/// For POSIX-based systems, this uses `calloc` and `free`. On Windows, it uses
diff --git a/lib/src/utf16.dart b/lib/src/utf16.dart
index 05cc1d2..fb35b3a 100644
--- a/lib/src/utf16.dart
+++ b/lib/src/utf16.dart
@@ -20,7 +20,7 @@
/// in the UTF-16 encoded result. See [Utf16Encoder] for details on encoding.
///
/// Returns a [allocator]-allocated pointer to the result.
- static Pointer<Utf16> toUtf16(String string, {Allocator allocator = malloc}) {
+ static Pointer<Utf16> toUtf16(String string, {Allocator allocator = calloc}) {
final units = string.codeUnits;
final Pointer<Uint16> result = allocator<Uint16>(units.length + 1);
final Uint16List nativeString = result.asTypedList(units.length + 1);
diff --git a/lib/src/utf8.dart b/lib/src/utf8.dart
index fe7b743..3360922 100644
--- a/lib/src/utf8.dart
+++ b/lib/src/utf8.dart
@@ -55,7 +55,7 @@
/// in the UTF-8 encoded result. See [Utf8Encoder] for details on encoding.
///
/// Returns a [allocator]-allocated pointer to the result.
- static Pointer<Utf8> toUtf8(String string, {Allocator allocator = malloc}) {
+ static Pointer<Utf8> toUtf8(String string, {Allocator allocator = calloc}) {
final units = utf8.encode(string);
final Pointer<Uint8> result = allocator<Uint8>(units.length + 1);
final Uint8List nativeString = result.asTypedList(units.length + 1);
diff --git a/test/utf16_test.dart b/test/utf16_test.dart
index 2b85764..f0935a1 100644
--- a/test/utf16_test.dart
+++ b/test/utf16_test.dart
@@ -5,7 +5,7 @@
import 'dart:ffi';
import 'dart:typed_data';
-import 'package:ffi/ffi.dart' hide allocate;
+import 'package:ffi/ffi.dart';
import 'package:test/test.dart';
void main() {
@@ -15,7 +15,7 @@
final Uint16List end = converted.asTypedList(start.codeUnits.length + 1);
final matcher = equals(start.codeUnits.toList()..add(0));
expect(end, matcher);
- malloc.free(converted);
+ calloc.free(converted);
});
test('toUtf16 emoji', () {
@@ -25,6 +25,6 @@
final Uint16List end = converted.cast<Uint16>().asTypedList(length + 1);
final matcher = equals(start.codeUnits.toList()..add(0));
expect(end, matcher);
- malloc.free(converted);
+ calloc.free(converted);
});
}
diff --git a/test/utf8_test.dart b/test/utf8_test.dart
index 80ebc0e..d4fc8db 100644
--- a/test/utf8_test.dart
+++ b/test/utf8_test.dart
@@ -9,7 +9,7 @@
import 'package:test/test.dart';
Pointer<Uint8> _bytesFromList(List<int> ints) {
- final Pointer<Uint8> ptr = malloc(ints.length);
+ final Pointer<Uint8> ptr = calloc(ints.length);
final Uint8List list = ptr.asTypedList(ints.length);
list.setAll(0, ints);
return ptr;
@@ -23,7 +23,7 @@
final matcher =
equals([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10, 0]);
expect(end, matcher);
- malloc.free(converted);
+ calloc.free(converted);
});
test('fromUtf8 ASCII', () {
@@ -41,7 +41,7 @@
final matcher =
equals([240, 159, 152, 142, 240, 159, 145, 191, 240, 159, 146, 172, 0]);
expect(end, matcher);
- malloc.free(converted);
+ calloc.free(converted);
});
test('formUtf8 emoji', () {