Re: Setting a hard limit on slice size, is this possible?

2021-08-06 Thread Paul Backus via Digitalmars-d-learn

On Friday, 6 August 2021 at 19:03:53 UTC, Tejas wrote:


Stealing Paul's answer now:
```d
import std;

enum your_max_length = 1000;
enum your_align = 256;
struct MySlice(T/*, size_t maxLength*/)
{
private align(your_align)T payload;

//invariant(payload.length <= maxLength);

//this(T[] slice) { payload = slice; }

//T opIndex(size_t i) { return payload[i]; }
}
void main()
{
   MySlice!(int/*, 1000*/)[your_max_length] slice;
writeln(slice.sizeof);
}
```


You can actually pass the alignment as a parameter too:

```d
struct Aligned(T, size_t alignment)
if (alignment >= T.alignof)
{
align(alignment) T payload;
alias payload this;
}

void main()
{
Aligned!(int, 16)[4] x;
assert(x.alignof == 16);
assert(x.sizeof == 64);

Aligned!(int[4], 16) y;
assert(y.alignof == 16);
assert(y.sizeof == 16);
}
```


invariants and compiler flags, best practice?

2021-08-06 Thread Bruce Carneal via Digitalmars-d-learn
I'm nervous enough about future compilations/builds of the code 
that I'm responsible for that I employ the following idiom quite 
a bit, mostly in @trusted code:


  (some boolean expression denoting invariants) || assert(0, 
"what went wrong");


How might the above cause problems and how do you deal with the 
possibility of someone disabling checking of one sort or another? 
 Do you embrace it as late-binding desirable?  Ignore it?  Other?




Re: Setting a hard limit on slice size, is this possible?

2021-08-06 Thread Tejas via Digitalmars-d-learn

On Friday, 6 August 2021 at 18:02:01 UTC, james.p.leblanc wrote:

mes
On Friday, 6 August 2021 at 17:25:24 UTC, Tejas wrote:


Okay we were overthinking the solution.

Just use a static array

```d
int[your_max_length]/*or whatever type*/ var;
```

You're good to go!

I almost feel stupid now lol


Hello Tejas,

Kind thanks for your replies ... all are appreciated.

However, do NOT feel stupid ... the motivation behind why
I cannot use a standard int[your_max_length] (in other words,
use a static array), is because I need to do a specified
memory alignment (known at compile time) on my slice, or array.

I understand that neither a slice or an array is capable to 
doing
an arbitrary memory alignment.  (But, perhaps I am wrong about 
this ...)


I believe structs can be aligned, but need to learn more about
the speicific of that.

In the meantime, I  have written a way to get an aligned slice 
from a
static array.  Ugly beginner code is below.  While I believe 
this basic
idea should work, I would like to guarantee that my slice does 
not get

moved in memory (destroying the alignment).

Ugly code here:

--

import std.stdio;

enum MAXLENGTH = 1024;
enum ALIGN = 128;

void main(){

   // static array to allow creation of aligned slice
   ubyte[MAXLENGTH+ALIGN] u;

   writeln("u.ptr:  ", u.ptr);
   auto u_i = cast(ulong) u.ptr;

   auto u_excess = u_i%ALIGN;
   writeln("u_excess: ", u_excess);

   ulong mustadd;
   if(u_excess !=0){
   mustadd = ALIGN-u_excess;
   }
   writeln("mustadd:  ", mustadd);

   // create aligned pointer for our needed slice
   auto zp = cast(double*) (u.ptr + mustadd);

   // create slice
   double[] z = zp[0 .. MAXLENGTH];
   writeln("z.ptr:  ", z.ptr);
   writeln("z.length:  ", z.length);
   auto z_i = cast(ulong) z.ptr;
   auto z_excess = z_i%ALIGN;
   writeln("z_excess:  ", z_excess);
}


If you're being that specialized, don't bother with fundamental 
data types, the hunt for simplicity will lead you to create very 
complex things.


As HS Teoh said, take a look at ```align```:
https://dlang.org/spec/attribute.html#align

Stealing Paul's answer now:
```d
import std;

enum your_max_length = 1000;
enum your_align = 256;
struct MySlice(T/*, size_t maxLength*/)
{
private align(your_align)T payload;

//invariant(payload.length <= maxLength);

//this(T[] slice) { payload = slice; }

//T opIndex(size_t i) { return payload[i]; }
}
void main()
{
   MySlice!(int/*, 1000*/)[your_max_length] slice;
writeln(slice.sizeof);
}
```


Re: Setting a hard limit on slice size, is this possible?

2021-08-06 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Aug 06, 2021 at 06:02:01PM +, james.p.leblanc via 
Digitalmars-d-learn wrote:
[...]
> However, do NOT feel stupid ... the motivation behind why
> I cannot use a standard int[your_max_length] (in other words,
> use a static array), is because I need to do a specified
> memory alignment (known at compile time) on my slice, or array.

I've never actually tried this myself, but have you looked into the
`align` keyword in the D spec?  I'm not 100% sure whether it can be
applied to static arrays (or arrays in general) but it might be worth a
look.


> I understand that neither a slice or an array is capable to doing
> an arbitrary memory alignment.  (But, perhaps I am wrong about this
> ...)
> 
> I believe structs can be aligned, but need to learn more about the
> speicific of that.

One thing that might help you is if you had a struct with the
appropriate align(...) declaration that wraps around your array
elements. Pretty sure that would guarantee the alignment of array
elements, which in turn should guarantee alignment of the array as a
whole.


T

-- 
It is the quality rather than the quantity that matters. -- Lucius Annaeus 
Seneca


Re: Setting a hard limit on slice size, is this possible?

2021-08-06 Thread james.p.leblanc via Digitalmars-d-learn

mes
On Friday, 6 August 2021 at 17:25:24 UTC, Tejas wrote:


Okay we were overthinking the solution.

Just use a static array

```d
int[your_max_length]/*or whatever type*/ var;
```

You're good to go!

I almost feel stupid now lol


Hello Tejas,

Kind thanks for your replies ... all are appreciated.

However, do NOT feel stupid ... the motivation behind why
I cannot use a standard int[your_max_length] (in other words,
use a static array), is because I need to do a specified
memory alignment (known at compile time) on my slice, or array.

I understand that neither a slice or an array is capable to doing
an arbitrary memory alignment.  (But, perhaps I am wrong about 
this ...)


I believe structs can be aligned, but need to learn more about
the speicific of that.

In the meantime, I  have written a way to get an aligned slice 
from a
static array.  Ugly beginner code is below.  While I believe this 
basic
idea should work, I would like to guarantee that my slice does 
not get

moved in memory (destroying the alignment).

Ugly code here:

--

import std.stdio;

enum MAXLENGTH = 1024;
enum ALIGN = 128;

void main(){

   // static array to allow creation of aligned slice
   ubyte[MAXLENGTH+ALIGN] u;

   writeln("u.ptr:  ", u.ptr);
   auto u_i = cast(ulong) u.ptr;

   auto u_excess = u_i%ALIGN;
   writeln("u_excess: ", u_excess);

   ulong mustadd;
   if(u_excess !=0){
   mustadd = ALIGN-u_excess;
   }
   writeln("mustadd:  ", mustadd);

   // create aligned pointer for our needed slice
   auto zp = cast(double*) (u.ptr + mustadd);

   // create slice
   double[] z = zp[0 .. MAXLENGTH];
   writeln("z.ptr:  ", z.ptr);
   writeln("z.length:  ", z.length);
   auto z_i = cast(ulong) z.ptr;
   auto z_excess = z_i%ALIGN;
   writeln("z_excess:  ", z_excess);
}


Re: Setting a hard limit on slice size, is this possible?

2021-08-06 Thread Tejas via Digitalmars-d-learn

On Friday, 6 August 2021 at 10:50:19 UTC, james.p.leblanc wrote:

I am aware of the "capacity" concept with slices.

But, I would like to know if it is possible to set a
hard limit on a slice size.

I prefer it to error and crash instead of a doing an
extension or reallocation.

I understand my question screams of "convoluted
thinking".  But, I need to align my slice according to
certain criteria.

(Alternatively, I could use an array ... if I could
align that according to criteria known at compile
time.  Is this possible?).

I have a working solution (ugly trick, maybe is a
better description) to align my slice as desired.

But, the solution would be more robust if I could
guarantee that the slice is never moved in memory.

Any thoughts, hints, etc are welcome!

James


Okay we were overthinking the solution.

Just use a static array

```d
int[your_max_length]/*or whatever type*/ var;
```

You're good to go!

I almost feel stupid now lol




Re: Setting a hard limit on slice size, is this possible?

2021-08-06 Thread Tejas via Digitalmars-d-learn

On Friday, 6 August 2021 at 17:16:28 UTC, Tejas wrote:

On Friday, 6 August 2021 at 16:32:59 UTC, james.p.leblanc wrote:

On Friday, 6 August 2021 at 11:58:59 UTC, Paul Backus wrote:

[...]

```

Paul,

Thanks very much for your reply.  I understand only
a fraction of the suggested solution.  I will need to
digest this a bit, and do some more background reading
on templates.

My initial, naive attempts to use this in a simple main()
were unsuccessful.  I'll keep plugging at it ...

Best Regards,
James


If you want something even simpler, maybe this could help:

```d
void setCapacity(int[] a)// not int a[]

```



Re: Setting a hard limit on slice size, is this possible?

2021-08-06 Thread Tejas via Digitalmars-d-learn

On Friday, 6 August 2021 at 16:32:59 UTC, james.p.leblanc wrote:

On Friday, 6 August 2021 at 11:58:59 UTC, Paul Backus wrote:

struct MySlice(T, size_t maxLength)
{
private T[] payload;
invariant(payload.length <= maxLength);
this(T[] slice) { payload = slice; }
T opIndex(size_t i) { return payload[i]; }
// etc.
}

```

Paul,

Thanks very much for your reply.  I understand only
a fraction of the suggested solution.  I will need to
digest this a bit, and do some more background reading
on templates.

My initial, naive attempts to use this in a simple main()
were unsuccessful.  I'll keep plugging at it ...

Best Regards,
James


If you want something even simpler, maybe this could help:

```d
void setCapacity(int/*or whatever type you want*/ a[], ulong 
capacity){

assert(capacity < 100/*whatever you want to set*/);
a.length = capacity;
}

void main(){
 int[] arr;
 setCapacity(arr, 50);//works :D
 //setCapacity(arr, 1000);// fails :(
}
```
Of course, this won't work if you want to resize the capacity 
natively, ie, by using ```.length``` directly; then you will have 
to use Paul's solution.


If you still have difficulties, please ping.

Also, if you're completely new, please try the book "programming 
in D", it is a marvellous resource.

link:
http://ddili.org/ders/d.en/


Re: Setting a hard limit on slice size, is this possible?

2021-08-06 Thread james.p.leblanc via Digitalmars-d-learn

On Friday, 6 August 2021 at 11:58:59 UTC, Paul Backus wrote:

struct MySlice(T, size_t maxLength)
{
private T[] payload;
invariant(payload.length <= maxLength);
this(T[] slice) { payload = slice; }
T opIndex(size_t i) { return payload[i]; }
// etc.
}

```

Paul,

Thanks very much for your reply.  I understand only
a fraction of the suggested solution.  I will need to
digest this a bit, and do some more background reading
on templates.

My initial, naive attempts to use this in a simple main()
were unsuccessful.  I'll keep plugging at it ...

Best Regards,
James


Re: Generating C Headers From D Code

2021-08-06 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 5 August 2021 at 16:28:35 UTC, Jack Stouffer wrote:

I know the compiler has C++ header generation


Your best bet is probably to use this and just do the slight 
modifications to turn it from C++ into C.


dmd -HC

followed by a modification script to rip some of the stuff out 
might get you somewhere.


A long time ago i made a program called dtoh that did it from the 
generated json but like i think that broke years ago, i haven't 
touched it for ages. can't find it anymore. dmd -HC surely better 
anyway.




Re: Generating C Headers From D Code

2021-08-06 Thread Jack Stouffer via Digitalmars-d-learn

On Thursday, 5 August 2021 at 17:02:33 UTC, Tejas wrote:

On Thursday, 5 August 2021 at 16:28:35 UTC, Jack Stouffer wrote:
I need to generate plain C99 .h files from a D module's 
extern(C) declarations, so that I can link a DMD generated .o 
file with a C code base. Are there any automated tools which 
do this?


I know the compiler has C++ header generation, and there's 
tons of tools which exist for importing C headers into D code. 
I'm not aware of anything which goes the other direction. 
Google wasn't much help either.


I also can't find anything... until someone else comes with a 
better answer, maybe you can do this:


Use the ```-H``` compiler flag to generate ```.di``` files.

Remove all the ```extern(C)``` decls in the .di files.

Rename the file extension from ```.di``` to ```.h```

Technically, it should work. Hopefully someone else knows 
better.


Well, that's disappointing. I suppose everyone just makes there 
main file a D file when converting C projects so they don't have 
this problem.


Eventually I'll have to write a script which takes .di files and 
generates .h files, but doing it manually will have to work for 
now.


Re: best/proper way to declare constants ?

2021-08-06 Thread someone via Digitalmars-d-learn

On Friday, 6 August 2021 at 04:57:02 UTC, Ali Çehreli wrote:

May I humbly suggest names like Location instead of 
structureLocation to make refactoring even more 
straightforward. ;)


just renamed everything like following:

- sWhatever for structures
- iWhatever for interfaces
- cWhatever for classes

- udtWhatever for initialized structures
- objWhatever for instantiated classes

... far better ;D


Tracy

2021-08-06 Thread JG via Digitalmars-d-learn
There was a message a while back 
(https://forum.dlang.org/post/fyakhpjbcpzqegfev...@forum.dlang.org)

about adding support for tracy.
When I asked a question about compile time performance I received 
the following instructions:

https://forum.dlang.org/post/eevoyuwhbuycyzgxs...@forum.dlang.org

I guess this means that tracy has been integrated?
If this is so is it documented anywhere how to use it?




Re: Setting a hard limit on slice size, is this possible?

2021-08-06 Thread Paul Backus via Digitalmars-d-learn

On Friday, 6 August 2021 at 10:50:19 UTC, james.p.leblanc wrote:

I am aware of the "capacity" concept with slices.

But, I would like to know if it is possible to set a
hard limit on a slice size.

I prefer it to error and crash instead of a doing an
extension or reallocation.


I don't think there's any way to make built-in slices behave like 
this, but you can pretty easily define your own wrapper type:


```d
struct MySlice(T, size_t maxLength)
{
private T[] payload;

invariant(payload.length <= maxLength);

this(T[] slice) { payload = slice; }

T opIndex(size_t i) { return payload[i]; }

// etc.
}
```


Setting a hard limit on slice size, is this possible?

2021-08-06 Thread james.p.leblanc via Digitalmars-d-learn

I am aware of the "capacity" concept with slices.

But, I would like to know if it is possible to set a
hard limit on a slice size.

I prefer it to error and crash instead of a doing an
extension or reallocation.

I understand my question screams of "convoluted
thinking".  But, I need to align my slice according to
certain criteria.

(Alternatively, I could use an array ... if I could
align that according to criteria known at compile
time.  Is this possible?).

I have a working solution (ugly trick, maybe is a
better description) to align my slice as desired.

But, the solution would be more robust if I could
guarantee that the slice is never moved in memory.

Any thoughts, hints, etc are welcome!

James





Re: Conditional compilation: Which version identifier for release code ? version(assert) ?

2021-08-06 Thread wjoe via Digitalmars-d-learn

On Thursday, 5 August 2021 at 11:54:38 UTC, Mike Parker wrote:

On Thursday, 5 August 2021 at 10:43:01 UTC, wjoe wrote:




Could you elaborate on ```version(assert)``` a bit more, 
please ? Like I compiled with ```-release, -g``` and without 
the 2 options but the ```assert``` branch was always taken. 
Could it be that ```-unittest``` has something to do with it ?


Yes, -unittest overrides -release and enables asserts. 
-check=assert=off overrides both.




It is sort of embarassing reading this after one night's sleep.
But the lesson learned was well worth it, thanks :)

So in light of all of this what I was looking for is 
```D_NoBoundsChecks``` which you mentioned initially.




Re: Conditional compilation: Which version identifier for release code ? version(assert) ?

2021-08-06 Thread wjoe via Digitalmars-d-learn

On Thursday, 5 August 2021 at 11:01:56 UTC, Adam D Ruppe wrote:

On Thursday, 5 August 2021 at 09:18:08 UTC, wjoe wrote:
If it's to be determined whether or not the code is being 
compiled in debug or release mode, i.e. e.g. the dmd 
```-release```


You should never use the -release flag. It should be renamed to 
"-enable-security-holes" since that's what it actually does.




This is good advice. Actually I had no intention to *use* the 
```-release``` option.
My question was aimed at getting an understanding of the 
different version identifiers and how they are affected by the 
command line options in order to be able to pick one that 
reflects the users expectations.

My wording should have been better. Sorry for the confusion.

Instead you can disable specific things as-needed, but it 
is probably never needed. These are also never supposed to 
actually change the behavior of your program, but in reality, 
like I said they do tend to change it - by enabling security 
holes.


However isn't the point of using version identifiers and these 
options to actually change the behavior of the program ?
I mean if bounds checking is enabled I expect the program to 
behave differently, i.e. to abort if something is out of bounds.