Re: byte array to string
On Thursday, 25 February 2021 at 06:58:51 UTC, FeepingCreature wrote: On Thursday, 25 February 2021 at 06:57:57 UTC, FeepingCreature wrote: On Thursday, 25 February 2021 at 06:47:11 UTC, Mike wrote: hi all, If i have an array: byte[3] = [1,2,3]; How to get string "123" from it? Thanks in advance. string str = format!"%(%s)"(array); Er sorry, typo, that should be "%(%s%)". "Print the elements of the array, separated by nothing." Compare "%(%s, %)" for a comma separated list. Thanks a lot. Keep playing.
byte array to string
hi all, If i have an array: byte[3] = [1,2,3]; How to get string "123" from it? Thanks in advance.
thread ring
hi all, Before more complex problem I've implement thread ring how I feel it as warmup fun. Comments and objections are very welcome. --- import std.stdio; import std.concurrency; void worker(int i) { Tid neib; int N; bool stop = false; receive((Tid message) { neib = message; }); while (!stop) { receive( (int message) { N = message; }, (Variant message) { stop = true; }); if (N == 0) { ownerTid().send(i); } else { neib.send(N-1); } } } void main () { Tid[503] w; // create for (int i = 0; i < w.length; i++) { w[i] = spawn(, i+1); } // set for (int i = 0; i < w.length - 1; i++) { w[i].send(w[i+1]); } w[$-1].send(w[0]); // start w[0].send(123456); receive( (int message) { writeln(message); } ); // 222 } ---
Proper desctructor for an class containing dynamic array of objects
Hi, my name is Mike and I'm new to D (coming from a Javabackground) and for fun I'm trying to learn D now. I created a simple class class Block { int a, b; this() {} } And now I have a dynamic array of objects of this class in another class: class Foo { Block[] array = new Block[](10); this() { for (int i = 0; i < array.length; i++) { array[i] = new Block(); } } } How would a proper destructor of class Foo look like? Is it enough to set "array" to null? Or do I have to set every element of the array to null and then the array, or nothing of that at all because the garbage collecter collects it, if the reference to Foo is set to null?
Re: Create class on stack
On Monday, 7 August 2017 at 13:42:33 UTC, Moritz Maxeiner wrote: You can still create a (scope) class on the stack, escape a reference to it using `move` and use it afterwards, all within the rules of @safe, so I'm not convinced that the reason for deprecating scoped classes is gone yet. Compare this to `scoped`, which behaves as expected (since it wraps the reference type object in a value type): Looks like a bug to me. I recommend submitting a bug report and tag it somehow with "scope" and/or "DIP1000". It appears Walter is giving any bugs with scope/DIP1000 priority. Mike
Re: Create class on stack
On Sunday, 6 August 2017 at 15:47:43 UTC, Moritz Maxeiner wrote: If you use this option, do be aware that this feature has been scheduled for future deprecation [1]. It's likely going to continue working for quite a while (years), though. [1] https://dlang.org/deprecate.html#scope%20for%20allocating%20classes%20on%20the%20stack FYI: http://forum.dlang.org/post/np1fll$ast$1...@digitalmars.com "Yes, it will have to be updated - but I didn't want to adjust it before DIP1000 spec is finalized. Rationale that was driving deprecation of scope storage class is becoming obsolete with DIP1000 implemented but not before." Mike
Re: Is std.xml seriously broken, or is it me?
On Sunday, 30 July 2017 at 02:58:09 UTC, Mike wrote: import std.xml; import std.stdio; void main() { auto parser = new DocumentParser("encoding=\"utf-8\"?>"); parser.onStartTag["device"] = (ElementParser parser) { writeln("device"); }; parser.parse(); } https://dpaste.dzfl.pl/262597d2fda6 I used it before without any trouble. Is it somehow seriously broken? If not, what am I doing wrong? It appears `onStartTag` does not handle the root element. For example, this code seems to work: import std.xml; import std.stdio; void main() { auto parser = new DocumentParser("encoding=\"utf-8\"?>"); parser.onStartTag["peripheral"] = (ElementParser parser) { writeln("peripheral"); }; parser.parse(); } Mike
Is std.xml seriously broken, or is it me?
I'm trying to use std.xml, and I can't get it to work. I tried the simplest program I could think of: import std.xml; import std.stdio; void main() { auto parser = new DocumentParser("encoding=\"utf-8\"?>"); parser.onStartTag["device"] = (ElementParser parser) { writeln("device"); }; parser.parse(); } https://dpaste.dzfl.pl/262597d2fda6 I used it before without any trouble. Is it somehow seriously broken? If not, what am I doing wrong? Thanks, Mike
Re: BetterC and TypeInfo Question
On Friday, 23 June 2017 at 02:14:08 UTC, Adam D. Ruppe wrote: Yes, it is necessary, but how much? Can we do it with implicitly generated library code? I'm pretty sure the answer is "not much" and "yes", but I still need to ponder the details. I think the typeinfo for a class good enough for dynamic cast could be about half the size we have now. Though like I said, a lot of the stuff we have now is cool stuff, so I don't want to miss it entirely, it could be behind another pointer. I'm not sure what you have in mind, but TypeInfo in itself is not bad and has some very useful features even for resource constrained devices and other niche domains. The problem is that the current compiler-runtime requires it to exist just to get a build, even though it's not being used in the source code (implicitly or otherwise) and has no chance of ever being executed or referenced in the resulting binary. Also, TypeInfo has been overused in the compiler-runtime implementation. For example: http://forum.dlang.org/post/mrmv61$230i$1...@digitalmars.com The compiler should be able to generate the object code in a way that allows the linker to discard it, if it can't find anything that uses it. I think LTO has made such features even more intelligent. I'd like to see 3 improvements: 1. The compiler doesn't require TypeInfo to be implemented in the runtime if the user's code will never make use of it. 2. If TypeInfo is implemented in the runtime, the compiler generates the object code in a way that permits the linker to discard it if it can't find a link to it. 3. If only one item is needed out of TypeInfo, pass a reference to that instead of the entire TypeInfo object in the compiler-runtime implementation, so implementations can be made more granular. Or perhaps TypeInfo should be broken up into a few smaller types. Mike
Re: how to allocate class without gc?
On Tuesday, 26 January 2016 at 01:09:50 UTC, Igor wrote: Is there any examples that shows how to properly allocate an object of a class type with the new allocators and then release it when desired? There are a number of different patterns discussed and illustrated with examples at http://wiki.dlang.org/Memory_Management. These don't use std.experimental.allocator, but should serve as a pretty good foundation for doing so. Mike
Re: std.experimental.logger
On Tuesday, 5 January 2016 at 02:44:48 UTC, sanjayss wrote: I'm doing the following: import std.experimental.logger; int main(string[] args) { sharedLog = new FileLogger("logfile.log"); log("Test log 1"); log("Test log 2"); log("Test log 3"); } and I expected the logs to be seen in the logfile.log, but it seems like my reading of the docs on this is incorrect and the logfile.log is not populated at all (though it is created). What am I missing or using incorrectly? Basically I am trying to have the default logger log to a file instead of stderr. (I am on a Mac (OS-X 10.11.1, 64 bit) and using DMD 2.069.2) You need to log with sharedLog: sharedLog.log("Test log 1"); sharedLog.log("Test log 2"); sharedLog.log("Test log 3");
Re: std.experimental.logger
On Tuesday, 5 January 2016 at 02:59:04 UTC, sanjayss wrote: On Tuesday, 5 January 2016 at 02:49:01 UTC, Mike wrote: [...] Thanks, that works. But the docs are confusing -- it gives the impression that "sharedLog" is something associated with the default logger -- so I would expect the above to work with just the plain log() call to invoke the default logger. But it seems like I am just creating a new logger and using that by saying "sharedLog.log()". From the doc: The default Logger will by default log to stderr and has a default LogLevel of LogLevel.all. The default Logger can be accessed by using the property called sharedLog. This property a reference to the current default Logger. This reference can be used to assign a new default Logger. sharedLog = new FileLogger("New_Default_Log_File.log"); You are right, according to the docs your example should've worked just fine. Tried it myself on DMD 2.069.2 and it doesn't work either. You should raise an issue on github.
Re: What does the -betterC switch in dmd do?
On Sunday, 15 November 2015 at 15:34:19 UTC, Jacob Carlborg wrote: I'm pretty sure that the only things that are excluded are module info and type info. It's still possible to use "new" and all the array features that requires support in the runtime (slicing, concatenation, appending and so on). In my experience, the only thing removed by -betterC is the ModuleInfo [1] However, there is a pending pull request to remove TypeInfo also [2] I don't think it was every fully implemented to the author's intention, and I'm not sure if it ever will be [3] Mike [1] http://forum.dlang.org/post/mailman.273.1386260316.3242.d@puremagic.com [2] https://github.com/D-Programming-Language/dmd/pull/5105 [3] http://forum.dlang.org/thread/lddug4$jgv$1...@digitalmars.com
Re: RAII and Deterministic Destruction
On Tuesday, 25 August 2015 at 22:35:57 UTC, Jim Hewes wrote: Although C++ can be ugly, one reason I keep going back to it rather then commit more time to reference-based languages like C# is because I like deterministic destruction so much. My question is whether D can REALLY handle this or not. I've not been sure about this for some time so now I'm just going to come out and finally ask. You may also find http://wiki.dlang.org/Memory_Management useful. It shows a number of different patterns one can employ for deterministic memory management. The one which you'll probably find most C++-like is http://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation Mike
Re: unusual bare metal target: Amazon Dash
On Tuesday, 18 August 2015 at 01:32:13 UTC, Laeeth Isharc wrote: I don't know whether D can run on one, but from a quick look perhaps feasible. Running D on something like this (perhaps it's underpowered, but looked to have similar spec to what people had been doing with related ARM cortex processors) would certainly make the point very vivid that it can be a bare metal programming language. Only 1Mb of flash RAM for the program - is that enough? Yes, with the right techniques. Everything one needs to get started is documented at http://wiki.dlang.org/Minimal_semihosted_ARM_Cortex-M_%22Hello_World%22 A more complete proof of concept on similar hardware can be found at https://github.com/JinShil/stm32f42_discovery_demo Mike
Re: What is the exact meaning of 'nothrow'?
On Thursday, 11 June 2015 at 00:27:36 UTC, Ali Çehreli wrote: Note: Remember that it is not recommended to catch Error nor its base class Throwable. What I mean by any exception here is any exception that is defined under the Exception hierarchy. A nothrow function can still emit exceptions that are under the Error hierarchy, which represents irrecoverable error conditions that should preclude the program from continuing its execution. In other words, nothrow means does not emit Exception, it can still emit Error. Feel free to donate those finely worded statements to the official docs. Mike
Re: What happens when you launch a D application ?
FYI, I didn't realize this (but just figured it out), C main *used* to be in druntime, but it's now generated by the compiler. See here: https://github.com/D-Programming-Language/dmd/blob/master/src/mars.c#L236 True. But it is compiler-dependent. GDC actually still defines C main in the runtime: https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/__entrypoint.di#L60 Mike
Re: What happens when you launch a D application ?
Could you explain what mean C main inside the runtime. I thought that is only one main is possible. And why it's named *С* main D is not C-translated language. Same question is about _Dmain -- what is it? If I will call this() before main? What it will be? Will it run before main? This is a great question; one which I've tried to answer through my own investigation. So, I'll add what I've learned. Every D program is started as if it were a C program. This is easiest to see with the GNU toolchain. Try to compile a blank test.c file gcc test.c /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../lib/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' Now try to compile a blank test.d file gdc test.d /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../lib/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' {...and a bunch of other errors} So, you can see that both C and D programs need to define an unmangled `main` symbol. We call this symbol C main. DMD and LDC seem to add C main automatically, while GDC defines it in the D runtime: https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/__entrypoint.di#L60 C main then calls `_d_run_main`, and passes in a pointer to `_DMain`. `_DMain` is actually a phony alias to your D program's `void main()`, so you won't find it implemented anywhere. Your D program's `void main()` is D-mangled, so it is distinguishable from C main. We call this symbol D main https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/__entrypoint.di#L62 https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/rt/dmain2.d#L235 _d_run_main calls `rt_init()` which does all of the D runtime initialization... https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/rt/dmain2.d#L410 ... and then calls `_DMain` (but it was passed into `_d_run_main as` `mainFunc`: https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/rt/dmain2.d#L411 So in summary it looks like this (At least in Linux): * Operating system calls `_start` which is defined in the C Runtime (crt1.o). * _start calls the unmangled `main` which we will call C main. This function is automatically added by DMD and LDC, but defined in GDC's D runtime. * C main calls `_d_run_main` passing in a pointer to the symbol `_DMain`. `_DMain` is actually your D program's `void main()` function. It is D-mangled, so the linker can distinguish it from C main * `_d_run_main` then calls `rt_init()` to initialize the runtime, and then calls `_DMain`. I believe the module constructors and the static constructors are called in D runtime's rt_init function: https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/rt/dmain2.d#L152 I'm still studying how exactly that works, though. I think they are linked into ModuleInfo. I hope this is helpful. If you want to know more about how the operating system calls C main (at least in a Linux environment), see these links: http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html https://gcc.gnu.org/onlinedocs/gccint/Initialization.html Mike
Re: What happens when you launch a D application ?
On Saturday, 23 May 2015 at 10:57:22 UTC, Suliman wrote: Every D program is started as if it were a C program. Why is so necessary? It's not actually necessary. You could implement the `_start` function in your D program. Here's a D program without any C runtime, D runtime, or main. long __d_sys_write(long arg1, in void* arg2, long arg3) { ulong result; asm { syscall : =a result : a 1, D arg1, S arg2, m arg2, d arg3 : memory, cc, rcx, r11; } return result; } extern(C) void __d_sys_exit(long arg1) { asm { syscall : : a 60, D arg1, : memory, cc, rcx, r11; } } extern(C) void _start() { // you could put this in a main function and // call main() from here. auto text = Hello, World!\n; __d_sys_write(1, text.ptr, text.length); __d_sys_exit(0); } Compile with: gdc -fno-emit-moduleinfo -nostartfiles test.d This produces a 733 byte Hello World binary. -fno-emit-moduleinfo will prevent the compiler from generating a lot of auxiliary code that's not needed for this program. -nostartfiles will prevent the C runtime from being linked in. This is a nice little experiment, but once you start trying to use structs, classes, arrays, exceptions, or any other feature of D, you will need more and more of the D runtime. There may also be some things generated by GDC that get called implicitly by the C runtime's initialization procedures (e.g. _init, _fini, etc..) but I'm not that knowledgeable about the compiler's codegen. What about C++ and other languages? Does they have more then one main? C++ is a superset of C, so in my experience, C++ only needs C main. However, C++ has static constructors that get called by some of the initialization procedures in the C runtime before actually calling main. I wonder if C++ free functions mangled in C++...I'm not sure. Why it's more than one main is needed? Why D apps can't start with single main? I don't think the current implementation is necessary. There are probably many different ways to implement D program initialization. I don't see why D runtime couldn't just implement _start, omit the C runtime altogether, and do everything in D, but I trust the compiler implementers have their reasons. If you're interested in digging deeper, you'll probably have to ask questions on D.GNU to get the attention of the compiler implementers. Mike
Re: Necessity of D Library (and/or Core Library)
On Saturday, 23 May 2015 at 06:35:50 UTC, Anthony Monterrosa wrote: Does D require the standard library to function? Or to be more direct, does D as a language need its library, or core library, to function correctly? There are two main libraries in D: The D Runtime, and the standard library (a.k.a. Phobos) By default, D links in Phobos and the D Runtime. With the GDC compiler, the two are actually compiled as a single binary. You can disable linking phobos and the D Runtime with the -nophoboslib compiler flag. I'm not sure if DMD or LDC offer a similar compiler option. You could, however, compile to object files and then link manually with the system linker to avoid linking in phobos and the D Runtime. If you don't explicitly import anything, then you won't need phobos, but you may need the D Runtime depending on which features of the language you use. However, if you stick to a C-like subset of D, you need very little of the D Runtime (50~100 lines of code) Note: since, I'm already here, does anyone know how D manipulates/uses standard streams to make its write/read functions as well? I can't find any resources telling me a non-abstracted way of this being completed. It is my understanding that D links in the C standard library and leverages the functionality there for standard streams. Mike
Re: how does 'shared' affect member variables?
On Saturday, 9 May 2015 at 20:17:59 UTC, bitwise wrote: On Sat, 09 May 2015 15:38:05 -0400, Mike n...@none.com wrote: On Saturday, 9 May 2015 at 18:41:59 UTC, bitwise wrote: Also, I wasn't able to find any thorough documentation on shared, so if someone has a link, that would be helpful. Here are a few interesting links: Iain Buclaw (lead developer for GDC) with his interpretation: http://forum.dlang.org/post/mailman.739.1431034764.4581.digitalmar...@puremagic.com Andrei Alexandrescu highlighting a critical flaw with `shared` http://forum.dlang.org/post/lruc3n$at1$1...@digitalmars.com The truth about shared http://p0nce.github.io/d-idioms/#The-truth-about-shared Interesting deprecation warning in latest compiler (See compiler output): http://goo.gl/EGvK72 But I don't know what the semantics are *supposed* to be, and I get the impression noone else knows either. I'll be watching this thread myself to see if someone can provide some insight. Mike So it seems that although it's not properly implemented, it's still not completely benign, right? I am trying to create a shared queue/array of delegates that run on the main thread. I don't know if D's 'shared' semantics will affect it or not, and whether or not casting to/from shared will cause problems with 'cas' or 'atomicStore' Would the following code work as expected? I'm not sure if it would or no, as I haven't used these features of D before, but it looks like what you are trying to implement is what `synchronized` already provides: http://ddili.org/ders/d.en/concurrency_shared.html#ix_concurrency_shared.synchronized Mike
Re: how does 'shared' affect member variables?
On Saturday, 9 May 2015 at 18:41:59 UTC, bitwise wrote: Also, I wasn't able to find any thorough documentation on shared, so if someone has a link, that would be helpful. Here are a few interesting links: Iain Buclaw (lead developer for GDC) with his interpretation: http://forum.dlang.org/post/mailman.739.1431034764.4581.digitalmar...@puremagic.com Andrei Alexandrescu highlighting a critical flaw with `shared` http://forum.dlang.org/post/lruc3n$at1$1...@digitalmars.com The truth about shared http://p0nce.github.io/d-idioms/#The-truth-about-shared Interesting deprecation warning in latest compiler (See compiler output): http://goo.gl/EGvK72 But I don't know what the semantics are *supposed* to be, and I get the impression noone else knows either. I'll be watching this thread myself to see if someone can provide some insight. Mike
Re: Startup files for STM32F4xx
On Saturday, 25 April 2015 at 16:32:50 UTC, Timo Sintonen wrote: On Saturday, 25 April 2015 at 11:56:55 UTC, Martin Nowak wrote: I have not yet needed anything from libc/phobos in my programs. I think there's a few gems that can be cherry-picked out of Phobos, especially for metaprogramming: std.traits std.typecons std.typetuple There are also a couple things in these libraries that might be useful for programming in general: std.conv std.string std.array Mike
Re: Startup files for STM32F4xx
On Saturday, 25 April 2015 at 19:33:05 UTC, Johannes Pfau wrote: volatileLoad is not in gdc yet. I've written the code some months ago but I need to update it and then it needs to be reviewed. It's officially in 2.067.0 for anyone who's wondering. Volatile!T: http://dpaste.dzfl.pl/dd7fa4c3d42b Volatile!size_t value; value += 1; assert(value == 1); Register wrapper: http://dpaste.dzfl.pl/3e6314714541 Register definition: enum Level : ubyte { low = 0, high = 1 } enum fields = [ Field(PIN0, 0, 0, true, Level, Access.readWrite), Field(PIN1, 1, 1, true, Level, Access.readWrite), Field(TEST, 2, 4, false, ubyte, Access.readWrite)]; mixin(generateRegisterType!ubyte(PORT, fields)); pragma(address, 0x25) extern __gshared PORTRegister PORTB; Usage: auto b = PORTB.load(); PORTB.toggle!PIN0; PORTB.PIN0 = Level.low; writeln(PORTB.PIN0); PORTB.TEST = 0b000; That's some nice code! and really leveraging D to great effect. I know that Volatile!(T) took some engineering to get right, so it would be nice to have that as an official type IMO. The remaining problem is performance. (With optimization the generated code is as good as equivalent C code. However, we need to avoid size overhead: e.g. struct initializers and the opX functions shouldn't generate functions in the executable, though tha can be fixed with the linker) I'm not sure I follow how the linker can solve this. Could you elaborate? Mike
Re: Startup files for STM32F4xx
On Saturday, 25 April 2015 at 00:33:26 UTC, Steven Schveighoffer wrote: http://www.digikey.com/product-search/en?x=0y=0lang=ensite=uskeywords=stm32f429+discovery This is super tempting @ $24. As someone who is not used to tinkering with raw hardware, how does one power this thing? I've tinkered with SBCs, but those had full-blown Linux. What hardware is necessary to get this thing running? -Steve It's powered from your PC's USB port (Using one right now, actually). No extra hardware is needed unless you want to add features beyond what the board provides. Due to its large number of pins, and the way they are arranged, they don't plug into breadboards, but you can easily use jumper wires for that: https://www.adafruit.com/products/153. I have a basic D demo for this board here: https://github.com/JinShil/stm32f42_discovery_demo. It's just a blinky app at the moment, but it does all the work of configuring the clocks, flash, etc... before calling the main function. There's quite a few Discovery boards from ST (http://www.st.com/web/en/catalog/tools/FM116/SC959/SS1532/LN1848?icmp=ln1848_pron_pr-stm32f446_dec2014sc=stm32discovery-pr) as well as boards from other manufacturers (http://www.mikroe.com/stm32/development-boards/). It's quite exciting to see Jens's work and the expressed interest in using D for this domain. I hope to see more. Mike
Re: Creating a microcontroller startup file
On Tuesday, 7 April 2015 at 20:33:26 UTC, Jens Bauer wrote: Question number 2: Is it possible to change the VectorFunc to be a real function pointer, rather than a void* ? I did something along these lines (modified to match your example) and it worked fine for me: alias VectorFunc = void function(); @attribute(weak) @attribute(alias, defaultHandler) extern void Reset_Handler(); @attribute(weak) @attribute(alias, defaultHandler) extern void NMI_Handler() @attribute(weak) @attribute(alias, defaultHandler) extern void HardFault_Handler(); @attribute(section,.isr_vector.ro) immutable ISR[3] g_pfnVectors = [ Reset_Handler , NMI_Handler , HardFault_Handler ]; I did this before weak, alias, and section attributes were added, however. To see my original code, look at the slide in the presentation here: https://youtu.be/o5m0m_ZG9e8?t=2332. My original code had everything decorated with extern(C) as well so I could refer to the symbol directly in my linker scripts. That may not be needed for you, so I left it out. Question number 3: How can I call an external function and keep the binary file size down ? Are you compiling with -ffunction-sections -fdata-sections and linking with --gc-sections? You may need to in order to get rid of some things. What do you using for your D runtime? Perhaps some code in your runtime is implicitly linking to some code you're not directly calling. I also add the following to my linker scripts to get rid of stuff I don't find necessary: /DISCARD/ : { *(.ARM.extab*) *(.ARM.exidx*) } /DISCARD/ : { *(.ARM.attributes*) *(.comment) } You can see the latest incarnation of my linker script here: https://github.com/JinShil/stm32f42_discovery_demo/blob/master/linker/linker.ld Question number 4: How can I reduce the function declaration of the Reset_Handler and NMI_Handler shown above ? Try something along these lines. enum weak = gcc.attribute.attribute(weak); enum isrDefault = gcc.attribute.attribute(alias, defaultHandler); extern @weak @isrDefault void NMI_Handler(); extern @weak @isrDefault void HardFault_Handler(); I use this idiom briefly in my code here: https://github.com/JinShil/stm32f42_discovery_demo/blob/master/source/start.d The enum thing kinda bugs me about D, but you'll see it used everywhere, especially phobos. It's called a manifest constant, and you can find a short blurb about it at the bottom of this page: http://dlang.org/enum.html Mike
Re: Creating a microcontroller startup file
On Wednesday, 8 April 2015 at 11:17:12 UTC, Mike wrote: On Tuesday, 7 April 2015 at 20:33:26 UTC, Jens Bauer wrote: Question number 2: Is it possible to change the VectorFunc to be a real function pointer, rather than a void* ? I did something along these lines (modified to match your example) and it worked fine for me: alias VectorFunc = void function(); [...] @attribute(section,.isr_vector.ro) immutable ISR[3] g_pfnVectors = [ Reset_Handler , NMI_Handler , HardFault_Handler ]; Sorry, but that code should be: @attribute(section,.isr_vector.ro) immutable VectorFunc[3] g_pfnVectors = [ Reset_Handler , NMI_Handler , HardFault_Handler ];
Re: Creating a microcontroller startup file
On Wednesday, 8 April 2015 at 16:10:53 UTC, Jens Bauer wrote: On Wednesday, 8 April 2015 at 11:17:12 UTC, Mike wrote: On Tuesday, 7 April 2015 at 20:33:26 UTC, Jens Bauer wrote: -I actually added @attribute(naked) to my defaultResetHandler yesterday, as I wanted to get rid of the prologue; so I completely agree; the startup code should have this attribute. I've now changed that to use the enum, to be more consistent. ;) I actually added that out of necessity, not optimization. Id I use the STM32, and reset the MCU, the CCRAM is disabled by default. Since my stack is in CCRAM, I need to first enable it before any functions can be called. Mike
Re: Creating a microcontroller startup file
On Wednesday, 8 April 2015 at 15:44:00 UTC, Jens Bauer wrote: On Wednesday, 8 April 2015 at 11:17:12 UTC, Mike wrote: I did something along these lines (modified to match your example) and it worked fine for me: alias VectorFunc = void function(); @attribute(weak) @attribute(alias, defaultHandler) extern void Reset_Handler(); Strange; I can't get it to build without extern(C). Also, if I remove extern(C) from for instance HardFault_Handler, then a HardFault_Handler written in C is not found by the linker. If HardFault_Handler is written in C then you will definitely need to decorate with extern(C). If your handlers are written in D, and neither the implementation nor the declaration are decorated with extern(C) then it should work. Mike
Re: Creating a microcontroller startup file
On Wednesday, 8 April 2015 at 17:45:01 UTC, Jens Bauer wrote: On Wednesday, 8 April 2015 at 15:53:37 UTC, Jens Bauer wrote: [snip] I find it strange that calling an empty function outside the source file will cause that huge difference. -But of course, there's a logic explanation somewhere. ;) It might be caused by the linker script; I'll try and see if I can modify it to get rid of those things. Nope, that wasn't it. However, I found out that when I call an external function, some unwinding code is forced upon me; it's used by libgcc. I can't seem to get rid of it. I've removed the -lgcc from my linker flags (along with *all* other libraries, and it's still forced upon me. I tried to remove as much of the druntime, as I could, but it did not help a tad. I can think of two places in the runtime where extra code can be added to your binary: runtime initialization and thread-local storage. There may be others depending on what features are implemented in the runtime. You can find the runtime initialization code for GDC's runtime here: https://github.com/D-Programming-GDC/GDC/blob/master/libphobos/libdruntime/rt/dmain2.d#L152 That eventually will call some things that malloc, and more. That all happens before you get to main. Are you compiling with -fno-emit-moduleinfo? That may help reduce some of that runtime initialization. As I recall, thread-local storage also employs malloc as memory is needed for each thread-local variable when a new thread is created. If your project is single-threaded, it still works the same way when the initial thread is created. If you have any thread-local state try removing them or changing them to __gshared, and see if that helps. My runtime is quite minimal, which has both benefits and consequences. You can find it here: https://github.com/JinShil/stm32f42_discovery_demo/tree/master/source/runtime This will give you a C-like programming experience. You can use classes, but won't be able to allocate them on the GC heap. Instead, you can employ some of the patterns here: http://wiki.dlang.org/Memory_Management You may also wnat to compile with -nodefaultlibs -nostdlib -nostartfiles. That removes the cruntime and libgcc. But if you do that, you may have to compensate by adding additional startup code In D. You can see how I've done that here: https://github.com/JinShil/stm32f42_discovery_demo/blob/master/source/start.d#L102 As I understand it, minlibd is a more full-featured runtime, and that too has its benefits and consequences. Mike
Re: Creating a microcontroller startup file
On Thursday, 9 April 2015 at 00:37:32 UTC, Jens Bauer wrote: On Wednesday, 8 April 2015 at 23:23:53 UTC, Mike wrote: I actually added that out of necessity, not optimization. Id I use the STM32, and reset the MCU, the CCRAM is disabled by default. Since my stack is in CCRAM, I need to first enable it before any functions can be called. According to ST-Microelectronics, CCMRAM is enabled by default (by hardware). I am using CCMRAM without enabling it, so it must be correct what their User's Manual states. Indeed, that's true. This problem I'm referring to only occurs when when resetting from the system boot loader. Since I want my stack to work under both conditions, I need to add that code. See the discussion here for more information: https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https%3a%2f%2fmy.st.com%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fcortex_mx_stm32%2fLeave%20DFU%20while%20boot0%20is%20high%20%28STM32F4%29FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5Bcurrentviews=1158 Mike
Re: Creating a microcontroller startup file
On Wednesday, 8 April 2015 at 23:23:53 UTC, Mike wrote: On Wednesday, 8 April 2015 at 16:10:53 UTC, Jens Bauer wrote: On Wednesday, 8 April 2015 at 11:17:12 UTC, Mike wrote: On Tuesday, 7 April 2015 at 20:33:26 UTC, Jens Bauer wrote: -I actually added @attribute(naked) to my defaultResetHandler yesterday, as I wanted to get rid of the prologue; so I completely agree; the startup code should have this attribute. I've now changed that to use the enum, to be more consistent. ;) I actually added that out of necessity, not optimization. Id I use the STM32, and reset the MCU, the CCRAM is disabled by default. Since my stack is in CCRAM, I need to first enable it before any functions can be called. Sorry, I need to be more careful when typing on a tablet. That should read: I actually added that out of necessity, not optimization. If I use the STM32 system bootloader, and reset the MCU, the CCRAM is disabled by default. Since my stack is in CCRAM, I need to first enable it before any functions can be called. Mike
static alias this
Consider this simple example A)- struct StaticRegister { static private uint _value; @property static uint value() { return _value; } @property static void value(uint v) { _value = v; } } void main(string[] s) { StaticRegister = 1; assert(StaticRegister == 1); } --- This gives two errors for each line in `main` (exactly what I expected). test.d(8): Error: StaticRegister is not an lvalue test.d(9): Error: incompatible types for ((StaticRegister) == (1)): cannot use '==' with types However, if I modify the example by adding an `alias this` ... B)- struct StaticRegister { static private uint _value; @property static uint value() { return _value; } @property static void value(uint v) { _value = v; } alias value this; } void main(string[] s) { StaticRegister = 1; assert(StaticRegister == 1); } --- ... the assignment error is eliminated, but the read is not. test.d(11): Error: incompatible types for ((StaticRegister) == (1)): cannot use '==' with types I argue that either both errors should be eliminated, or neither should be eliminated. One could also argue that some variation of the following should be required... * static alias value this; * alias static value this; * alias value static this; ... to distinguish it from non-static `this` Now, in the example below, `this` is referring to the type itself in a static context C)--- import std.stdio; struct StaticRegister { static string GetType() { return typeof(this).stringof; } } void main(string[] s) { writeln(StaticRegister.GetType()); } --- So, it follows that the example below should work... and it does D)--- struct StaticRegister { static private uint _value = 0; @property static uint value() { return _value; } @property static void value(uint v) { _value= v; } static uint GetValue() { return this.value; } } void main(string[] s) { assert(StaticRegister.GetValue() == 0); } --- So, why does `alias this` in a static context (See example B above) only half-work? Bug? If not, what's the design rationale? Thanks, Mike
What is RTInfo?
object.d [1] says it's infomation for the precise GC (which I thought wasn't implemented yet). It just seems to return a void*. But, searching the source code, it doesn't seem to be set by anything anywhere. So, what is RTInfo and what is its purpose. And how is it different from TypeInfo? Thanks, Mike [1] https://github.com/D-Programming-Language/druntime/blob/f0c1e13d8bd547eed517b1ae17f085966bb165c1/src/object.di#L180
Struggling with shared objects
Hey, all. I have just started using D and I'm really loving it, but I have been caught on a problem. I am trying to share my Client class to a new thread, and after a bit of struggling: I finally got the code to compile! Now I am having a new problem: the moment I call client.receive(buf) I get an error: D:\D\dmd2\src\phobos\std\concurrency.d(13,13): Error: static assert (false || false) is false (StompBroker) I assume it has something to do with the way I am handling the shared keyword along with pointers, but I cannot for the life of me figure this out! Any help is appreciated. Thank you! Code: https://github.com/mvlipka/StompBroker
Re: Struggling with shared objects
Ha, I am so stupid! I forgot to call the socket member before receive! Thank you for the help. I have also removed the pointers. I am now getting this: C:\Users\Michael\Documents\Projects\StompBroker\StompBrokerdmd main.d main.d(11): Error: None of the overloads of 'receive' are callable using a shared object, candidates are: D:\D\dmd2\windows\bin\..\..\src\phobos\std\socket.d(2897):std.socket.Socket.receive(void[] buf, SocketFlags flags) D:\D\dmd2\windows\bin\..\..\src\phobos\std\socket.d(2912):std.socket.Socket.receive(void[] buf)
Re: Struggling with shared objects
On Saturday, 17 January 2015 at 21:12:34 UTC, Vlad Levenfeld wrote: That's real weird. In D:\D\dmd2\src\phobos\std\concurrency.d(13,13) 13,13 isn't a line number and static assertions should go off during compilation, not runtime. It is during compilation. When I run dmd myself, I get this: C:\Users\Michael\Documents\Projects\StompBroker\StompBrokerdmd main.d D:\D\dmd2\windows\bin\..\..\src\phobos\std\concurrency.d(165): Error: static ass ert (false || false) is false D:\D\dmd2\windows\bin\..\..\src\phobos\std\concurrency.d(634): instantiat ed from here: checkops!(shared(Client*), char[1024]) main.d(11):instantiated from here: receive!(shared(Client*), char[1024])
Re: Struggling with shared objects
Oh, I said the moment I call I should have reworded that. I meant that the moment I add that line to the code, I get the error. Sorry!
Re: Struggling with shared objects
Thank you so much! I got it all working now. You're definitely right, that is still easier than C++.
Re: D Beginner Trying Manual Memory Management
On Monday, 12 January 2015 at 19:29:54 UTC, jmh530 wrote: I'm new to D. I have some modest knowledge of C++, but am more familiar with scripting languages (Matlab, Python, R). D seems so much easier than C++ in a lot of ways (and I just learned about rdmd today, which is pretty cool). I am concerned about performance of D vs. C++, so I wanted to learn a little bit more about manual memory management, in case I might ever need it (not for any particular application). There is a good article on the D Wiki that covers this topic with several different patterns and working examples: http://wiki.dlang.org/Memory_Management I hope you'll find it helpful. Mike
druntime vararg implementation
Greetings, In core.varar. (https://github.com/D-Programming-Language/druntime/blob/master/src/core/vararg.d), why is the X86 implementation singled out and written in D rather than leveraging the standard c library implementation like the others? Mike
Re: druntime vararg implementation
On Wednesday, 5 November 2014 at 14:24:00 UTC, Sean Kelly wrote: On Wednesday, 5 November 2014 at 09:45:50 UTC, Mike wrote: Greetings, In core.varar. (https://github.com/D-Programming-Language/druntime/blob/master/src/core/vararg.d), why is the X86 implementation singled out and written in D rather than leveraging the standard c library implementation like the others? No idea. It seems like a pointless duplication of code. Maybe just to have some documented functions within the module? In that case, a better question is Why use the standard C implementation if we have working D code?.
Re: Problem with spec doc?
On Saturday, 1 November 2014 at 15:02:44 UTC, Shriramana Sharma via Digitalmars-d-learn wrote: Hello. I already posted this via the forum interface: http://forum.dlang.org/thread/yqhwwpskwmkdefarj...@forum.dlang.org But for whatever reason I didn't get any replies so I worry if it actually reached people or not... -- Original message -- At http://dlang.org/spec.html I read: This is also available as a PDF document where PDF document is a link to http://dlang.org/dlangspec.pdf. However, the PDF is just as 4 page doc with only the TOC. The individual pages don't seem to have PDFs either. I hope this is not intentional. Can the full spec please be posted as a single PDF? Thanks. This pull request may answer your question: https://github.com/D-Programming-Language/dlang.org/pull/687 Mike
Re: Problem with spec doc?
On Saturday, 1 November 2014 at 15:48:27 UTC, Shriramana Sharma via Digitalmars-d-learn wrote: Thank you very much. But this is curious -- isn't Andrei himself able to directly commit or answer that pull request?!! All pull requests need to be peer reviewed, even those submitted by committers. Even if the pull request is merged, it won't appear on the website until it is published. From what I can tell, it appears the website is published with each release of DMD. Andrei was kind enough to post a link to a working copy of dlang.org on his own personal website. You should be able to obtain the PDF from there. Mike
forum.dlang.org open source?
Does forum.dlang.org have an open source repository somewhere that we can contribute pull requests to, or are bug reports to recommended procedure. I see that the left navigation menu needs updating, and I think we can wordsmith the forum descriptions a little to make it clearer where to post things. Some other nice features like backtick(`) markup and the ability to collapse/expand threads are also on my mind at the moment. Thanks for the support, Mike
Re: forum.dlang.org open source?
On Monday, 27 October 2014 at 02:24:04 UTC, MachineCode wrote: On Sunday, 26 October 2014 at 23:57:41 UTC, Mike wrote: Are you going to use HTML in the posts? because neither Walter and probably forum maintainer wants to use it. I don't plan on adding HTML to the posts. And you don't need to worry about my ideas ruining anything because they have to pass the scrutiny of the repository owner and the D community at large before they will be accepted.
Re: How to build dlang.org dd files
On Sunday, 31 August 2014 at 05:41:58 UTC, Mike wrote: I've been trying to update some documentation on dlang.org. The instructions at https://github.com/D-Programming-Language/dlang.org/blob/master/CONTRIBUTING.md say I should be able to do make -f posix.make file.html This doesn't seem to work, as I get no rule t omake target 'file.html'. I tried 'make -f posix.mak' and that requires dmd, druntime, and maybe a mess of other things, and takes forever. And ultimately it fails with 'No rule to make target 'release'. Stop.' Isn't there any easy one-liner I can use to render a single .dd file to html to test my changes? Thanks, Mike This seems to do it: dmd -c -o- macros.ddoc doc.ddoc -Df{ddoc_filename}.html {ddoc_filename}.dd If someone knows of a more official syntax, please let me know. Mike
How to build dlang.org dd files
I've been trying to update some documentation on dlang.org. The instructions at https://github.com/D-Programming-Language/dlang.org/blob/master/CONTRIBUTING.md say I should be able to do make -f posix.make file.html This doesn't seem to work, as I get no rule t omake target 'file.html'. I tried 'make -f posix.mak' and that requires dmd, druntime, and maybe a mess of other things, and takes forever. And ultimately it fails with 'No rule to make target 'release'. Stop.' Isn't there any easy one-liner I can use to render a single .dd file to html to test my changes? Thanks, Mike
Re: clear works better than it ought to considered i never defined it
On Monday, 28 July 2014 at 06:27:44 UTC, Vlad Levenfeld wrote: A weird thing happened: I was building a dictionary struct which contains a custom array of values and a differently-typed custom array of keys. Both of them implicitly define clear by aliasing a backing array. The dictionary type doesn't have clear, though. And it doesn't alias anything. Yet when I call clear on it, not only does it work, but it clears both of the private contained arrays. I can't find any free clear function in Phobos. Not that I'm complaining, but... what's going on here? I'm not sure, but is your code calling the runtime `clear` method: https://github.com/D-Programming-Language/druntime/blob/master/src/object.di#L519 It has been marked for deprecation in the upcoming 2.066 release. Mike
Contributing to D language
I wish I could help with the development of D (either the compiler or std library). Is there a TODO list kept somewhere? Neither Phobos nor DMD have an `issues` page on Github.. I found this http://wiki.dlang.org/Review_Queue but it's kind of short. Best regards, Mike
Re: Working on a library: request for code review
Do you think it's ready for a v0.1 release? It be willing to add it to dub if it passes general D coding stardards. Once again, thanks for feedback and vast improvements of the code. Best, Mike
Re: Working on a library: request for code review
Once again thanks for the feedback. The RLE rewrite using std is impressive! I will use it today or tomorrow, great stuff.
Re: Working on a library: request for code review
Thanks, will work on fixes tonight. The current method will not detect an error when the image type is not mapped but a color map is present. At least I assume that is not a valid TGA file? A non-mapped image may contain a color map ;-0 it's simply discarded. As per your ideas return to!bool(isColorMapped(header) ? header.colorMapDepth.among(16, 32) : header.pixelDepth.among(16, 32)); this is not nothrow, but as you can see there is nothing that can ever throw.. so perhaps I'll stick to the cast(bool) thing.. Any idea?
Re: Working on a library: request for code review
I have refactored the code as recommended. I have also modified the not-yet-reviewed writers part to take advantage of the same approach (preallocated static-sized buffer) rather than allocate slices in loops. Hoping to hear something from you guys! Best, Mike
Re: Working on a library: request for code review
On Friday, 13 June 2014 at 21:09:23 UTC, Mike Wey wrote: On 06/12/2014 09:30 PM, Rene Zwanenburg wrote: I remember a function which does something like only only + canFind on one go. It would look something like header.colorMapDepth.among(16, 32); but I can't find it right now.. Maybe it was only proposed but never added. http://dlang.org/library/std/algorithm/among.html Will apply this as well, thanks :-)
Re: Working on a library: request for code review
On Thursday, 12 June 2014 at 19:30:06 UTC, Rene Zwanenburg wrote: I need to go. Please don't mind any typo's and untested code ;). Didn't take a look at writers yet, readers and util need some more scrutiny, but the main theme is: eliminate unnecessary temporary GC allocations. Thank you for feedback, hoping for more! I will apply the suggestions tonight.
Re: Working on a library: request for code review
On Thursday, 12 June 2014 at 00:20:28 UTC, cal wrote: Might it be worth stitching things together into a proper image processing package? Well I started working on TGA because I was disappointed that no image abstraction is present in Phobos. Go has some imaging APIs and I think D would benefit from having one too (out of the box). Would I work on std.image? Sure. Best, Mike
Re: Working on a library: request for code review
I can shed some light on my, beginners, point of view and the rationale behind this tiny library. I want to create a bar/matrix-code generator (QR, DataMatrix etc.). I really like graphics-related subjects and I thought this would be a good start in D. Coming from Java island and having experience in Go I expected to find some basic imaging functionalities in the standard library: not necessary support for all image formats, but at least some bitmap i/o and data model (Pixel, Image, Filter ...). I found none of that :-( IMHO (one of the) pain(s) of C++ is that the stdlib is far behind what a modern developer would consider elementary. But I expected something from D, because Phobos is already more than C++'s stdlib is: Phobos has e.g. digests. IMO digests are not (as) necessary for a standard library as, say, a qsort is. So if there are digests, why not imaging? Either way, D is really nice and if I can help, I will :-) So far - the TGA lib. Best, Mike
Working on a library: request for code review
Hello. I am new to D and I must admit I really like the language. In my opinion it takes the best from C++ and, say, Python and combines it really elegantly. Great work! I am currently working on my first library in D - related to TARGA image format. Here's the link to the repo: http://bit.ly/1mIuGhv It's a work-in-progress case: at the moment the library does what I need for my other projects, but there is a couple of things that I want to add/fix soon. Perhaps someone could have a look at the code and point out some obvious traps that I fell into etc. Any feedback would be great! Best regards, Mike
Multi-file project problem with Visual D
I've created a small Windows based program using a win32 library I found online. That part is working just fine. I've also created a second file (called util.d) and within it defined a class called MyData (with a member function called Read()). Within my main program I instantiate and try to call the Read() method. No errors are generated but nothing gets called. Right now the Read() method just does a throw so I know something is going on. What am I missing here? I would think the compiler would generate an error if something was missing. winmain.d: - extern(Windows) LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: f.Read() ; return 0 ; /* ... */ } // WndProc util.d: -- class MyData { public: void Read() { throw new Exception(No!, __FILE__, __LINE__) ; } // void Read() } // class Data
Re: Multi-file project problem with Visual D
On Monday, 2 June 2014 at 01:01:22 UTC, Mike wrote: I've created a small Windows based program using a win32 library I found online. That part is working just fine. I've also created a second file (called util.d) and within it defined a class called MyData (with a member function called Read()). Within my main program I instantiate and try to call the Read() method. No errors are generated but nothing gets called. Right now the Read() method just does a throw so I know something is going on. What am I missing here? I would think the compiler would generate an error if something was missing. winmain.d: - extern(Windows) LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: f.Read() ; return 0 ; /* ... */ } // WndProc util.d: -- class MyData { public: void Read() { throw new Exception(No!, __FILE__, __LINE__) ; } // void Read() } // class Data Doing more testing and debugging found that the value of the instance is 0, which leads me to believe something is happening with the instantiation. Back to the books I suppose.