Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to migrate c to d07

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the methods of transplanting c to d07". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what are the methods of transplanting c to d07"?

The operating system returns a null * pointer with GetProcAddress/dlsym, one of which is.

FooPtr = cast (fooPtr) getPtr (); / / variable, used as the type.

And then go like this:

Alias int function () da_fooPtr

Da_fooPtr fooPtr = cast (da_fooPtr) getPtr ()

Therefore, it is better to use an alias. Da _ for d aliases. You can also:

Int foo ()

{

Return 1

}

Void* getPtr ()

{

Return cast (void*) & foo

}

Void bindFunc (void** func)

{

* func = getPtr ()

}

Void main ()

{

Int function () fooPtr

BindFunc (cast (void**) & fooPtr)

} / / convert foo** to (empty * *)

The second eliminates the alias .dmd that previously did not provide stack tracing. However, when dmd compiles binary, whether it is a shared library / exe, the configuration file is pre-configured to export all symbols. Otherwise, the trace stack cannot be implemented. But it makes my function pointer conflict with the function pointer that exports the library. Even shared libraries that are manually loaded. So we have to go back to the alias. Alias function pointers / variables are not exported. If you make dynamic binding, be sure to pay attention to this. I still use null * to load the function pointer because it is simpler.

Foo = cast (da_Foo) getSymbol ("foo")

/ / you can't see it

/ / you saw it.

Foo = bindFunc (cast (void**) & foo, "foo")

Do it manually, the latter copy and paste faster. There is one more thing. Given a dynamically bound function pointer, it is necessary to follow the storage variable rule of d. D variable, which defaults to thread local, that is, each thread has a copy of the variable.

If it is loaded in one thread and called in another thread, it will crash.

Fortunately, the function pointer of d is initialized to null by default, and the solution is to share across threads. One of the goals of using shared/__gshared.d is to make parallelism easier. Through sharing, it can be shared across threads. The compiler will say, this is not thread-safe access, as always, sharing is transitive, you reference a shared variable, the reference is shared. When using _ _ gshared, it is a global variable, and the programmer is responsible for synchronization. Therefore, when implementing dynamic binding, you should decide to use a line book? Share? Global sharing?.

_ _ gshared is generally used because threads have to access it. At this point, make sure that their own lifetimes are longer than visitors, so they are generally loaded / unloaded in static module constructors and destructors.

Extern (C)

{

Alias void function (int) da_foo

Alias int function () da_bar

}

_ _ gshared

{

Da_foo foo

Da_bar bar

}

How to load the library. I implemented the abstract loading library and took symbols in DerelictUtil. Externally I use free functions to load.

Binding takes effort, dynamic binding is more tiring, others like static binding, but parsing c is a problem. I prefer dynamic binding.

At this point, I believe you have a deeper understanding of "what are the methods of transplanting c to d07". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report