112 Commits

Author SHA1 Message Date
dec05eba
c0fa5963ee Make lastIndexOf use the same cut-off between BMH as indexOf
Also update test to use a string longer than 52 characters
to test both BMH and linear path.
2020-10-25 21:17:22 -04:00
Andrew Kelley
b0684bf084 std.mem: expose the simpler linear functions
The new defaults that came in with
644400054c5769a397ded4f569e2ac0600d65305 are nice, however, it is still
possible that someone knows their inputs are always small and wants to
use the simpler implementations. We keep the default to make the choice
at runtime, but expose the linear functions in the public interface of
std.mem.

Also improved the doc comments.
2020-10-17 19:24:52 -07:00
Andrew Kelley
644400054c
Merge pull request #6259 from dec05eba/master
Use boyer-moore-horspool algorithm for indexOfPos and lastIndexOf unless the haystack or needle is very small
2020-10-17 21:38:50 -04:00
Jan Prudil
aadccc4206 Make std.meta.Int accept a signedness parameter 2020-10-17 14:09:59 +02:00
dec05eba
ff58f09b68 Use better haystack size and needle size for cutoff between linear and BMH 2020-09-06 16:23:18 +02:00
dec05eba
8af1f8ba1a Add comment to clearify why the first/last element in preprocess is
skipped
2020-09-05 21:15:44 +02:00
dec05eba
0a016e8fc2 Fix indexOf and lastIndexOf with empty needle 2020-09-05 16:55:32 +02:00
dec05eba
f93498d2d8 Fix start index if @sizeOf(T) > 1 2020-09-05 15:31:22 +02:00
dec05eba
3a6e6bd538 Check if the type has unique bit representation to see if it can be compared byte-wise 2020-09-05 15:20:48 +02:00
dec05eba
db51821a97 Remove type size check, looks like its not needed
Add check if the type is float. Float byte comparison doesn't work
because +0.0 and -0.0 are considered equal but their byte
representations are not equal.
2020-09-05 14:56:59 +02:00
dec05eba
997451da03 Make preprocess function take comptime sized slice 2020-09-05 14:25:44 +02:00
dec05eba
a394a6300c Fix lastIndexOf and add tests that do not fallback to linear search
Use sliceAsBytes to convert []const T to []const u8.
Cleanup.
2020-09-05 14:17:38 +02:00
dec05eba
f65f3d24f8 Only use boyer-moore-horsepool for types that are power of 2 2020-09-05 12:57:05 +02:00
dec05eba
d012507a8f Use boyer-moore-horspool with all types of T 2020-09-05 12:02:18 +02:00
dec05eba
e297b4815c Create skipping table
Also fallback to naive implementation if haystack is small
or if the needle is small or if sizeof type is not 1.
2020-09-05 11:24:05 +02:00
dec05eba
50c52e0135 Use boyer-moore-horspool algorithm for indexOfPos and lastIndexOf 2020-09-05 11:22:12 +02:00
Vexu
1df0f3ac24
update uses of deprecated type field access 2020-09-03 18:10:40 +03:00
Cléo Rebert
fc001110b4 Added edge case test to mem.count
Some implementations break on this edge case. Thought relevant to add it.
2020-08-27 04:12:28 -04:00
Sobeston
7d0bb0774e std.mem.count 2020-08-26 17:37:05 -04:00
Frank Denis
ad18078d53 forceEval() -> doNotOptimizeAway() 2020-08-26 10:50:34 +02:00
Frank Denis
0bd53dd203 Rename blackBox, move it to std.mem.forceEval() 2020-08-26 10:50:34 +02:00
Andrew Kelley
9cfcd0c296
Merge pull request #6103 from Vexu/extern
Disallow extern variables with initializers.
2020-08-20 18:35:31 -04:00
Andrew Kelley
4a69b11e74 add license header to all std lib files
add SPDX license identifier
copyright ownership is zig contributors
2020-08-20 16:07:04 -04:00
Vexu
adc5bce5e8
translate-c: correct translation of global variables
* externs with intializers are translated as exports
* non extern without explicit initialization are zero initalized
2020-08-20 10:08:27 +03:00
Dmitry Atamanov
a9590f3bf8
Support tuples in mem.len and trait.isIndexable (#5897) 2020-08-14 01:14:32 +03:00
Andrew Kelley
cd6cdd0a75 std.mem.Allocator: add return_address to the interface
The high level Allocator interface API functions will now do a
`@returnAddress()` so that stack traces captured by allocator
implementations have a return address that does not include the
Allocator overhead functions. This makes `4` a more reasonable default
for how many stack frames to capture.
2020-08-08 00:34:13 -07:00
Andrew Kelley
cc17f84ccc std: introduce GeneralPurposeAllocator
`std.GeneralPurposeAllocator` is now available. It is a function that
takes a configuration struct (with default field values) and returns an
allocator. There is a detailed description of this allocator in the
doc comments at the top of the new file.

The main feature of this allocator is that it is *safe*. It
prevents double-free, use-after-free, and detects leaks.

Some deprecation compile errors are removed.

The Allocator interface gains `old_align` as a new parameter to
`resizeFn`. This is useful to quickly look up allocations.

`std.heap.page_allocator` is improved to use mmap address hints to avoid
obtaining the same virtual address pages when unmapping and mapping
pages. The new general purpose allocator uses the page allocator as its
backing allocator by default.

`std.testing.allocator` is replaced with usage of this new allocator,
which does leak checking, and so the LeakCheckAllocator is retired.

stage1 is improved so that the `@typeInfo` of a pointer has a lazy value
for the alignment of the child type, to avoid false dependency loops
when dealing with pointers to async function frames.

The `std.mem.Allocator` interface is refactored to be in its own file.

`std.Mutex` now exposes the dummy mutex with `std.Mutex.Dummy`.

This allocator is great for debug mode, however it needs some work to
have better performance in release modes. The next step will be setting
up a series of tests in ziglang/gotta-go-fast and then making
improvements to the implementation.
2020-08-07 22:45:45 -07:00
Andrew Kelley
d159ba9295 zig fmt 2020-08-04 23:04:20 -07:00
meme
f050150ffa Add memory replacement routines
* Added `replace` for simple pre-allocated search-and-replace
* Added `replacementSize` for calculating ahead-of-time buffer sizes for
performing a safe search-and-replace
* Added `replaceOwned` for automatically allocating and performing
replacement
2020-07-26 05:42:38 +00:00
Vexu
a1e78d0b06
add is_tuple field to struct typeinfo
part of #4335
2020-07-17 00:15:34 +03:00
Vexu
06c08e5219
std.mem.zeroes use std.mem.set instead of @memset
stage1 comptime is not smart enough to remeber the size of the casted
item which leads to out of bounds errors.
2020-07-16 17:05:14 +03:00
Vexu
8fe076daaf
std.mem.zeroInit support initiating with tuples 2020-07-16 16:00:42 +03:00
Isaac Freund
ef17af1270 std: add mem.joinZ
currently the only options are doing a second allocation and copying or
implementing this yourself.
2020-07-12 22:17:40 +00:00
xackus
5b570bceb5 document a few functions in std.mem 2020-07-12 21:55:49 +00:00
Vexu
e85fe13e44
run zig fmt on std lib and self hosted 2020-07-11 20:41:19 +03:00
Alexandros Naskos
30ae7f7573 Corrected default value field initialization in std.zeroInit 2020-07-01 23:09:08 +00:00
Jonathan Marler
c2eead9629 Fix issue 5741, use after free 2020-06-28 18:05:18 -04:00
Jonathan Marler
a728436992 new allocator interface after Andrew Kelley review 2020-06-27 08:57:35 -06:00
Jonathan Marler
dc9648f868 new allocator interface 2020-06-26 13:34:48 -06:00
Andrew Kelley
109c0b9d96 rename std.mem.defaultInit to std.mem.zeroInit 2020-06-01 14:47:18 -04:00
Alexis Brodeur
c0e5eca6f2 Add initialization helper
When using C libraries, C99 designator list initialization is often
times used to initialize data structure.

While `std.mem.zeroes` and manually assigning to each field can
achieve the same result, it is much more verbose then the equivalent
C code:

```zig
usingnamespace @cImport({
    @cInclude("sokol_app.h");
});

// Using `std.mem.zeroes` and manual assignment.
var app_desc = std.mem.zeroes(sapp_desc);
app_desc.init_cb = init;
app_desc.frame_cb = frame;
app_desc.cleanup_cb = cleanup;
app_desc.width = 400;
app_desc.height = 300;
app_desc.window_name = "no default init";

// Using `std.mem.defaultInit`.
var app_desc = std.mem.defaultInit(sapp_desc, .{
    .init_cb = init,
    .frame_cb = frame,
    .cleanup_cb = cleanup,
    .width = 400,
    .height = 300,
    .window_name = "default init"
});
```

The `std.mem.defaultInit` aims to solve this problem by zero
initializing all fields of the given struct to their zero, or default
value if any.  Each field mentionned in the `init` variable is then
assigned to the corresponding field in the struct.

If a field is a struct, and an initializer for it is present, it is
recursively initialized.
2020-06-01 14:45:35 -04:00
data-man
49dd2cbd9a Support vectors in mem.len 2020-05-27 00:00:19 +05:00
data-man
db0c30446a Supports vectors in mem.zeroes 2020-05-24 20:48:29 -04:00
Andrew Kelley
dd05f2be80 run zig fmt on std lib 2020-05-24 10:04:09 -04:00
Andrew Kelley
69a5f0d797 Merge remote-tracking branch 'origin/master' into self-hosted-incremental-compilation 2020-05-16 01:26:18 -04:00
Andrew Kelley
81a01bd481 fix codegen of sentinel-terminated arrays and .got alignment
we now have an exit(0) program working
2020-05-14 16:34:04 -04:00
Andrew Kelley
080022f6c6 self-hosted: fix compile errors, except for codegen.zig 2020-05-13 20:06:01 -04:00
Vexu
453df1cc1e
Merge pull request #4892 from Sobeston/patch-4
mem.zeroes - add sentinel terminated array support
2020-05-08 22:37:27 +03:00
Vexu
336ddb5b76
std: add test for mem.zeroes on sentinel terminated arrays 2020-05-08 19:03:27 +03:00
Jonathan Marler
0a76e11617 add failAllocator to enable some regression tests 2020-05-06 23:56:48 -06:00