Re: Allocating byte aligned array

2017-09-27 Thread timvol via Digitalmars-d-learn
On Wednesday, 27 September 2017 at 21:44:48 UTC, Ali Çehreli 
wrote:

On 09/27/2017 02:39 PM, timvol wrote:

[...]


void main() {
auto mem = new ubyte[1024+15];
auto ptr = cast(ubyte*)(cast(ulong)(mem.ptr + 15) & 
~0x0FUL);

auto arr = ptr[0..1024];
}

Ali


Works perfect. Thank you!


Allocating byte aligned array

2017-09-27 Thread timvol via Digitalmars-d-learn

Hi guys,
how can I allocate an (e.g. 16) byte aligned array?
In C I can do the following:

void *mem = malloc(1024+15);
void *ptr = ((uintptr_t)mem+15) & ~ (uintptr_t)0x0F;
memset_16aligned(ptr, 0, 1024);
free(mem);

I think in D it looks similar to this:

auto mem = new ubyte[1024+15];
auto ptr = (mem.ptr + 15) & ~ (...???...) 0x0F;

How to fill ...???... ?
Without a cast, the compiler tells me that I the types are 
incompatible (ubyte* and int).


What's the correct syntax here?

PS: I don't want to use the experimental branch that provides 
alignedAllocate().


spawnProcess: Exit parent process without terminating child process

2017-08-25 Thread timvol via Digitalmars-d-learn

Hi guys,

I want execute a process. I know, I can execute a process using 
"spawnProcess" or "executeShell". But I want exit the parent.


My code for testing purposes is the following:

int main(string[] asArgs_p)
{
if ( (asArgs_p.length >= 2) && asArgs_p[1].isDir() )
{
while(1) {}
}
else
{
import std.process;
spawnProcess([asArgs_p[0], "test"]);
}
return 0;
}

So, starting the application without any parameter, it calls 
"spawnProcess" with an parameter. Now, I want that the parent 
process (the process started without parameter) terminates, while 
the created process remains running.


At the moment, the parent process creates the child and remains 
open (because of the while(1)-loop).


Any ideas how I can exit the parent and keep the child process 
running?


Re: Read conditional function parameters during compile time using __traits

2017-06-23 Thread timvol via Digitalmars-d-learn

On Wednesday, 21 June 2017 at 20:48:52 UTC, ag0aep6g wrote:

On 06/21/2017 09:39 PM, timvol wrote:
 size_t calcLength(ubyte ubFuncCode)() if ( ubFuncCode == 
1 )

 {
 return 10; // More complex calculated value
 }

 size_t calcLength(ubyte ubFuncCode)() if ( ubFuncCode == 
2 )

 {
 return 20; // More complex calculated value
 }

 size_t calcLength(ubyte ubFuncCode)() if ( ubFuncCode == 
3 )

 {
 return 30; // More complex calculated value
 }

[...]
But... how can I execute these functions? I mean, calling 
doCalcLength(1) function says "Variable ubFuncCode cannot be 
read at compile time". So my idea is to create an array during 
compile time using traits (e.g. __traits(allMembers)) and to 
check this later during runtime. For illustration purposes 
something like this:


--> During compile time:

void function()[ubyte] calcLengthArray;

auto tr = __traits(allMembers, example);
foreach ( string s; tr )
{
 calcLengthArray[__trait(get, s)] = s;
}


As far as I know, there's no way to get the ubFuncCode from the 
constraints. In order to figure out which values are valid, you 
have to try them all. Which is actually doable for a ubyte:



size_t function()[ubyte] calcLengthArray;
static this()
{
import std.meta: aliasSeqOf;
import std.range: iota;
foreach (ubFuncCode; aliasSeqOf!(iota(ubyte.max + 1)))
{
static if (is(typeof(!ubFuncCode)))
{
calcLengthArray[ubFuncCode] = 
!ubFuncCode;

}
}
}


Using a static constructor instead of direct initialization, 
because you can't initialize a static associative array 
directly.



--> During runtime:

size_t doCalcLength(ubyte ubFuncCode)
{
 auto length = 0;

 if ( ubFuncCode in calcLengthArray )
 {
 length = calcLengthArray[ubFuncCode]!(ubFuncCode)();
 }

 return length;
}


If you can accept hard-coding the range of ubFuncCode values 
here (and if there are no holes), then you can generate a 
switch that calls the correct calcLength version:



import std.meta: aliasSeqOf;
import std.range: iota;
enum min = 1;
enum max = 3;
sw: switch (ubFuncCode)
{
foreach (code; aliasSeqOf!(iota(min, max + 1)))
{
case code:
length = calcLength!code();
break sw;
}
default: throw new Exception("unexpected ubFuncCode");
}


Instead of hard-coding the range, you could also do the same 
here as above when filling calcLengthArray: loop over all ubyte 
values and figure out which ones are valid with a `static if`.


I hope everyone knows what I want to do :). But... does anyone 
know how I can realize that? I don't want to use a switch/case 
structure because the calcLength() functions can be very 
complex and I've over 40 different function codes. So, I think 
the best approach is to use something similar to the one I 
described.


I don't see how you're reducing the complexity here. You have 
the same code, just spread over 40 functions, plus the extra 
code to make it work. From what I see, I'd prefer the 
hand-written switch.


Thanks in advance! I finally solved my problem by adjust my 
message structure. So I'm now sending the length of the message, 
followed by the function code and the data.


I finally created different functions and annotated them with 
@FunctionCode(1), e.g.:


@FunctionCode(1)
void func1() { ... }

@FunctionCode(2)
void func2() { ... }

But I now ran into the next problem.
I'm creating an array containing these function using traits:

#1: foreach ( cb; __traits(allMembers, test))
#2: {
#3: static if ( __traits(isStaticFunction, 
__traits(getMember, test, cb)) )

#4: {
#5: // do something
#6: }
#7: }

But I'm getting the following error on line #3: error: undefined 
identifier '_D11TypeInfo_ya6__initZ'


I figured out that some other users are also had this error. 
Unfortunately, the error wasn't solved perfectly as far as I 
know. So, how can I resolve the eror?


Read conditional function parameters during compile time using __traits

2017-06-21 Thread timvol via Digitalmars-d-learn
Hi! I've a simple array of bytes I received using sockets. What I 
want to do is to calculate the target length of the message. So, 
I defined a calcLength() function for each function code (it's 
the first byte in my array). My problem is that I defined the 
calcLength() function using conditions so that each calcLength 
should be called depending on the respective function code, see 
below:


module example;

private
{
size_t calcLength(ubyte ubFuncCode)() if ( ubFuncCode == 1 )
{
return 10; // More complex calculated value
}

size_t calcLength(ubyte ubFuncCode)() if ( ubFuncCode == 2 )
{
return 20; // More complex calculated value
}

size_t calcLength(ubyte ubFuncCode)() if ( ubFuncCode == 3 )
{
return 30; // More complex calculated value
}
}

size_t doCalcLength(ubyte ubFuncCode)
{
return calcLength!(ubFuncCode)();
}

int main()
{
doCalcLength(1);
return 0;
}

But... how can I execute these functions? I mean, calling 
doCalcLength(1) function says "Variable ubFuncCode cannot be read 
at compile time". So my idea is to create an array during compile 
time using traits (e.g. __traits(allMembers)) and to check this 
later during runtime. For illustration purposes something like 
this:


--> During compile time:

void function()[ubyte] calcLengthArray;

auto tr = __traits(allMembers, example);
foreach ( string s; tr )
{
calcLengthArray[__trait(get, s)] = s;
}

--> During runtime:

size_t doCalcLength(ubyte ubFuncCode)
{
auto length = 0;

if ( ubFuncCode in calcLengthArray )
{
length = calcLengthArray[ubFuncCode]!(ubFuncCode)();
}

return length;
}

I hope everyone knows what I want to do :). But... does anyone 
know how I can realize that? I don't want to use a switch/case 
structure because the calcLength() functions can be very complex 
and I've over 40 different function codes. So, I think the best 
approach is to use something similar to the one I described.


Re: cmake-d and gdc/gdmd compiler

2017-04-14 Thread timvol via Digitalmars-d-learn

On Tuesday, 4 April 2017 at 19:59:19 UTC, Dragos Carp wrote:

On Tuesday, 4 April 2017 at 18:42:45 UTC, timvol wrote:

Hi guys,

I'm trying to cross-compile a project using CMake and gdc (or 
better: the gdmd port). My CMakeLists-file is the following:




For cross-compiling, CMake uses a so called "toolchain" file 
[1] where you define the system paths similar to the target 
system.


I confess that never cross-compiled any code with cmake-d, but 
in principle it should work.


Dragos

[1] 
https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling-for-linux


I tried using a toolchain file but unfortunately, I'm still 
getting the same error. Using the dmd compiler works fine but not 
the GDC compiler for cross compiling :(.

Any other idea(s) what can cause the error?


Re: cmake-d and gdc/gdmd compiler

2017-04-05 Thread timvol via Digitalmars-d-learn

On Tuesday, 4 April 2017 at 19:59:19 UTC, Dragos Carp wrote:

On Tuesday, 4 April 2017 at 18:42:45 UTC, timvol wrote:

Hi guys,

I'm trying to cross-compile a project using CMake and gdc (or 
better: the gdmd port). My CMakeLists-file is the following:




For cross-compiling, CMake uses a so called "toolchain" file 
[1] where you define the system paths similar to the target 
system.


I confess that never cross-compiled any code with cmake-d, but 
in principle it should work.


Dragos

[1] 
https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling-for-linux


Alright. Thank you Dragos. I thought it's possible without using 
a toolchain file but I'll give it a try :)


cmake-d and gdc/gdmd compiler

2017-04-04 Thread timvol via Digitalmars-d-learn

Hi guys,

I'm trying to cross-compile a project using CMake and gdc (or 
better: the gdmd port). My CMakeLists-file is the following:


cmake_minimum_required(VERSION 2.8.1)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} 
"${CMAKE_SOURCE_DIR}/cmake-d")

SET(CMAKE_SYSTEM_NAME Linux)

project(myapp D)
set(CMAKE_D_FLAGS "${CMAKE_D_FLAGS} ${GLOBAL_DMD_DEFS}")

set(SOURCES
main.d
generic.d
)

include_directories(.)

add_executable(myapp ${SOURCES})

I'm working on Windows and I want compile the project for the 
target linux system. So, I downloaded the gdc (x86_64-linux-gnu), 
extracted it and set the PATH variable. Unfortunately, when I 
configure my project using CMake, I'm getting the following 
message:


CMake Error at 
C:/Users/User/Desktop/Project/cmake-d/CMakeDetermineDCompiler.cmake:32 (MESSAGE):

  Could not find compiler set in environment variable C:

  CMAKE_D_COMPILER-NOTFOUND.
Call Stack (most recent call first):
  CMakeLists.txt:6 (project)


CMake Error: CMAKE_D_COMPILER not set, after EnableLanguage

I think CMake cannot find the compiler because the executables of 
gdc are named like "x86_64-unknown-linux-gnu-gdc". So, I've added 
the following to my CMakeLists-file:


set(CMAKE_D_COMPILER x86_64-unknown-linux-gnu-gdmd)

But this also fails with the following error:

The D compiler identification is GNU
Check for working D compiler: 
C:/Users/User/Compiler/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-gdmd.exe
Check for working D compiler: 
C:/Users/User/Compiler/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-gdmd.exe -- broken

To force a specific D compiler set the DC environment variable
ie - export DC="/usr/bin/dmd"
CMake Error at 
C:/Users/User/Desktop/cmake-d/CMakeTestDCompiler.cmake:45 
(message):

  The D compiler
  
"C:/Users/User/Compiler/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-gdmd.exe"

  is not able to compile a simple test program.

  It fails with the following output:

   Change Dir: 
C:/Users/User/Desktop/Project.linux/CMakeFiles/CMakeTmp




  Run Build Command:"C:/Users/User/Compiler/ninja.exe"
  "cmTC_365e1"

  [1/2] Building D object CMakeFiles\cmTC_365e1.dir\testDCompiler


  FAILED: CMakeFiles/cmTC_365e1.dir/testDCompiler



  
C:\Users\User\Entwicklung\Compiler\x86_64-unknown-linux-gnu\bin\x86_64-unknown-linux-gnu-gdmd.exe

  CMakeFiles\cmTC_365e1.dir\testDCompiler -c
  
C:\Users\User\Desktop\Project.linux\CMakeFiles\3.7.2\CMakeTmp\testDCompiler.d




  x86_64-unknown-linux-gnu-gdc.exe: error:
  CMakeFiles\cmTC_365e1.dir\testDCompiler.d: No such file or 
directory





  x86_64-unknown-linux-gnu-gdc.exe: fatal error: no input files




  compilation terminated.




  Compile of ["CMakeFiles\\cmTC_365e1.dir\\testDCompiler.d"] 
failed




  ninja: build stopped: subcommand failed.






  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:7 (project)


Configuring incomplete, errors occurred!
See also 
"C:/Users/User/Desktop/Project.linux/CMakeFiles/CMakeOutput.log".


Does anyone know what can cause the problem here? I've also set 
the DC environment variable but I'm still getting the errors :(